Get File Time Information - Delphi

DarkCoderSc personal avatar
DarkCoderSc

Jean-Pierre LESUEUR

uses
  uHelper, System.SysUtils, Winapi.Windows;

// ...

procedure GetFileTime(const AFileName : String; var ACreate, ALastModified, ALastAccess : TDateTime);
begin
  var hFile := CreateFileW(
    PWideChar(AFileName),
    GENERIC_READ,
    FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
    nil,
    OPEN_EXISTING,
    FILE_FLAG_BACKUP_SEMANTICS,
    0
  );
  if hFile = INVALID_HANDLE_VALUE then
    raise EWindowsException.Create('GetFileTime');
  try
    var AFtCreate, AFtLastAccess, AFtLastModified : TFileTime;

    if not Winapi.Windows.GetFileTime(hFile, @AFtCreate, @AFtLastAccess, @AFtLastModified) then
      raise EWindowsException.Create('GetFileTime');

    ACreate       := TryFileTimeToDateTime(AFtCreate);
    ALastModified := TryFileTimeToDateTime(AFtLastModified);
    ALastAccess   := TryFileTimeToDateTime(AFtLastAccess);
  finally
    CloseHandle(hFile);
  end;
end;


// ...

begin
  try
    var ATargetFile := 'C:\Windows\Regedit.exe';
    ///

    var ACreate, ALastModified, ALastAccess : TDateTime;

    GetFileTime(ATargetFile, ACreate, ALastModified, ALastAccess);

    WriteLn(Format('Create: %s', [DateTimeToStr(ACreate)]));
    WriteLn(Format('Last Modified: %s', [DateTimeToStr(ALastModified)]));
    WriteLn(Format('Last Access: %s', [DateTimeToStr(ALastAccess)]));
  except
    on e : Exception do
      WriteLn(e.Message);
  end;

// ...

Creating and researching code snippets takes time and effort. You’re welcome to share them through your own platforms, but please don’t forget to credit the original author, here: Jean-Pierre LESUEUR.

Depends On


Created

September 17, 2025

Last Revised

September 17, 2025