Delphi 获取文件访问时间,也主是最后一次打开文件 的时间,基于Delphi7实现。相关代码如下:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
Edit2.Text := '';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Edit1.Text<>'' then
Edit2.Text := GetDateTime(Pchar(OpenDialog1.FileName));
end;
function TForm1.GetDateTime(const FileName: Pchar): string;
var
hFile : THandle;
Sy : TSecurityAttributes;
WTime, LTime : TFILETIME;
STime : TSystemTime;
begin
Sy.nLength := SizeOf(TSecurityAttributes);
//必需参数
Sy.lpSecurityDescriptor := nil;
Sy.bInheritHandle:=False;
hFile := CreateFile(PChar(FileName),GENERIC_READ , 1 ,@Sy,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 1);
if hFile=INVALID_HANDLE_VALUE then
Result:=''
else
begin
GetFileTime(hFile, nil, @WTime, nil);
FileTimeToLocalFileTime(WTime, LTime);
FileTimeToSystemTime(LTime, STime);
Result:=Format('%d年%d月%d日 %2.2d:%2.2d:%2.2d',[STime.wYear,STime.wMonth,STime.wDay,STime.wHour,STime.wMinute,STime.wSecond]);
end;
end;