Beranda > Delphi > When was that file last accessed?

When was that file last accessed?

Here’s an example of how to write a function that’ll return a file’s last access time (not to be confused with the last modified time).

function GetFileLastAccessTime(sFileName: string): TDateTime;
var
  ffd : TWin32FindData;
  dft : DWord;
  lft : TFileTime;
  h   : THandle;
begin
  // get file information
  h := Windows.FindFirstFile(PChar(sFileName), ffd);
  if INVALID_HANDLE_VALUE <> h then
  begin
    // we're looking for just one file, so close our "find"
    Windows.FindClose(h);

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

    // convert FILETIME to DOS time
    FileTimeToDosDateTime(lft, LongRec(dft).Hi, LongRec(dft).Lo);

    // finally, convert DOS time to TDateTime for use in Delphi's
    // native date/time functions
    Result := FileDateToDateTime(dft);
  end;
end;

GetFileLastAccessTime() will return a given file’s last access time as a Delphi TDateTime type which you can convert to a string by using the DateTimeToStr() function. For example:

MessageDlg(
    'c:\config.sys was last accessed on '
    + DateTimeToStr(GetFileLastAccessTime('c:\config.sys')),
    mtInformation, [mbOk], 0
);
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: