给出几个与文件时间相关的函数,供参考:
// 取得文件创建时间
function GetFileCreationTime(const FileName: String): TDateTime;
var
FileTime: TFileTime;
LocalFileTime: TFileTime;
hFile: THandle;
SystemTime: TSystemTime;
begin
Result := 0;
FileTime.dwLowDateTime := 0;
FileTime.dwHighDateTime := 0;
hFile := FileOpen(FileName, fmShareDenyNone);
try
if hFile <> 0 then
begin
Windows.GetFileTime(hFile, @FileTime, nil, nil);
FileTimeToLocalFileTime(FileTime, LocalFileTime);
FileTime := LocalFileTime;
end;
finally
FileClose(hFile);
end;
if FileTimeToSystemTime(FileTime, SystemTime) then
Result := SystemTimeToDateTime(SystemTime);
end;
// 取得文件修改时间
function GetFileLastWriteTime(const FileName: String): TDateTime;
var
FileTime: TFileTime;
LocalFileTime: TFileTime;
hFile: THandle;
SystemTime: TSystemTime;
begin
Result := 0;
FileTime.dwLowDateTime := 0;
FileTime.dwHighDateTime := 0;
hFile := FileOpen(FileName, fmShareDenyNone);
try
if hFile <> 0 then
begin
Windows.GetFileTime(hFile, nil, nil, @FileTime);
FileTimeToLocalFileTime(FileTime, LocalFileTime);
FileTime := LocalFileTime;
end;
finally
FileClose(hFile);
end;
if FileTimeToSystemTime(FileTime, SystemTime) then
Result := SystemTimeToDateTime(SystemTime);
end;
// 取得文件访问时间
function GetFileLastAccessTime(const FileName: String): TDateTime;
var
FileTime: TFileTime;
LocalFileTime: TFileTime;
hFile: THandle;
SystemTime: TSystemTime;
begin
Result := 0;
FileTime.dwLowDateTime := 0;
FileTime.dwHighDateTime := 0;
hFile := FileOpen(FileName, fmShareDenyNone);
try
if hFile <> 0 then
begin
Windows.GetFileTime(hFile, nil, @FileTime, nil);
FileTimeToLocalFileTime(FileTime, LocalFileTime);
FileTime := LocalFileTime;
end;
finally
FileClose(hFile);
end;
if FileTimeToSystemTime(FileTime, SystemTime) then
Result := SystemTimeToDateTime(SystemTime);
end;