在Delphi中,可以使用以下代码来获取文件的修改时间:
uses
SysUtils, Windows;
function GetFileModifyTime(const AFileName: string): TDateTime;
var
FindData: TWin32FindData;
FileTime: TFileTime;
LocalTime: TSystemTime;
begin
if FileExists(AFileName) then
begin
ZeroMemory(@FindData, SizeOf(TWin32FindData));
if FindFirstFile(PChar(AFileName), FindData) <> INVALID_HANDLE_VALUE then
begin
FileTime := FindData.ftLastWriteTime;
FileTimeToLocalFileTime(FileTime, FileTime);
FileTimeToSystemTime(FileTime, LocalTime);
Result := SystemTimeToDateTime(LocalTime);
FindClose;
end
else
begin
Result := 0;
end;
end
else
begin
raise Exception.Create('File not found');
end;
end;
使用上述函数可以获取指定文件的修改时间,例如:
var
ModifyTime: TDateTime;
begin
ModifyTime := GetFileModifyTime('C:\example.txt');
ShowMessage('File modify time: ' + DateTimeToStr(ModifyTime));
end;