频道分类

Delphi 上次访问该文件的时间

作者:admin 来源: 日期:2020/2/10 19:46:34 人气: 标签:

 
function GetFileLastAccessTime(sFileName: string): TDateTime;
var
  ffd : TWin32FindData;
  dft : DWord;
  lft : TFileTime;
  h   : THandle;
begin
  // get file information
  h := Winapi.Windows.FindFirstFile(PChar(sFileName), ffd);
  if INVALID_HANDLE_VALUE <> h then
  begin
    Winapi.Windows.FindClose(h);

    // convert the FILETIME to local FILETIME
    FileTimeToLocalFileTime(ffd.ftLastAccessTime, lft);

    FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);

    Result := FileDateToDateTime(dft);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
MessageDlg(
    'c:\config.sys was last accessed on '
    + DateTimeToStr(GetFileLastAccessTime('c:\vcredist_x86.log')),
    mtInformation, [mbOk], 0
);
end;