在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;