某些软件需要获取文件的最后被访问时间,这一属性是DOS文件系统所没有的,无法用传统的函数来做到。Windows 提供一个函数 GetFileTime 做此项操作,在 Delphi 中可方便地调用,示例如下:procedure GetFileLastAccessTime(FileName: PChar); var CreateFT, LastAccessFT, LastWriteFT: TFileTime; ST: TSystemTime; F: Integer; begin { 首先要用Windows的标准API函数以读方式打开文件 } F := CreateFile(FileName, GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if F=INVALID_HANDLE_VALUE then begin ShowMessage(’Can not open file!’); Exit; end; { 取文件时间 } if GetFileTime(F, @CreateFT, @LastAccessFT, @LastWriteFT) then begin { 转换为系统时间并显示 } FileTimeToSystemTime(LastAccessFT, ST); Label1.Caption := Format(’%d-%d-%d, %d:%d:%d’, [ST.wYear, ST.wMonth, ST.wDay, ST.wHour, ST.wMinute,ST.wSecond]); end; CloseHandle(F); { 记住关闭文件 } end;
取得文件的大小:UnitSystemCategoryI/O routinesfunction FileSize(var F): Integer; 取得文件的类型:Returns the extension portions of a file name.UnitSysUtilsCategoryfile name utilitiesfunction ExtractFileExt(const FileName: string): string;DescriptionUse ExtractFileExt to obtain the extension from a file name. For example, the following code returns the extension of the file name specified by a variable named MyFileName:MyFilesExtension := ExtractFileExt(MyFileName);
TimeType,参数为 0,1,2/分别为:// 分别为获取文件创建,更改,最后修改时间function TForm1.FGetFileTime(sFileName: string; TimeType: Integer): TDateTime; var ffd:TWin32FindData; dft:DWord; lft,Time:TFileTime; H:THandle; begin H:=Windows.FindFirstFile(PChar(sFileName),ffd); case TimeType of 0: Time:=ffd.ftCreationTime; 1: Time:=ffd.ftLastWriteTime; 2: Time:=ffd.ftLastAccessTime; end; //获取文件信息 if (H <> INVALID_HANDLE_VALUE) then begin //我们只查找一个文件,所以关掉"find" Windows.FindClose(H); //转换FILETIME格式成为local FILETIME格式 FileTimeToLocalFileTime(Time,lft); //转换FILETIME格式成为DOStime格式 FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo); //最后,转换DOStime格式成为Delphi's应用的TdateTime格式 Result:=FileDateToDateTime(dft); end else result:=0; end;
--------------------------------------------------------------------------------
某些软件需要获取文件的最后被访问时间,这一属性是DOS文件系统所没有的,无法用传统的函数来做到。Windows 提供一个函数 GetFileTime 做此项操作,在 Delphi 中可方便地调用,示例如下:procedure GetFileLastAccessTime(FileName: PChar);
var
CreateFT, LastAccessFT, LastWriteFT: TFileTime;
ST: TSystemTime;
F: Integer;
begin
{ 首先要用Windows的标准API函数以读方式打开文件 }
F := CreateFile(FileName, GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if F=INVALID_HANDLE_VALUE then
begin
ShowMessage(’Can not open file!’);
Exit;
end;
{ 取文件时间 }
if GetFileTime(F, @CreateFT, @LastAccessFT, @LastWriteFT) then
begin
{ 转换为系统时间并显示 }
FileTimeToSystemTime(LastAccessFT, ST);
Label1.Caption := Format(’%d-%d-%d, %d:%d:%d’,
[ST.wYear, ST.wMonth, ST.wDay, ST.wHour, ST.wMinute,ST.wSecond]);
end;
CloseHandle(F); { 记住关闭文件 }
end;
取得文件的大小:UnitSystemCategoryI/O routinesfunction FileSize(var F): Integer;
取得文件的类型:Returns the extension portions of a file name.UnitSysUtilsCategoryfile name utilitiesfunction ExtractFileExt(const FileName: string): string;DescriptionUse ExtractFileExt to obtain the extension from a file name. For example, the following code returns the extension of the file name specified by a variable named MyFileName:MyFilesExtension := ExtractFileExt(MyFileName);
TimeType: Integer): TDateTime;
var
ffd:TWin32FindData;
dft:DWord;
lft,Time:TFileTime;
H:THandle;
begin
H:=Windows.FindFirstFile(PChar(sFileName),ffd);
case TimeType of
0: Time:=ffd.ftCreationTime;
1: Time:=ffd.ftLastWriteTime;
2: Time:=ffd.ftLastAccessTime;
end; //获取文件信息
if (H <> INVALID_HANDLE_VALUE) then
begin
//我们只查找一个文件,所以关掉"find"
Windows.FindClose(H);
//转换FILETIME格式成为local FILETIME格式
FileTimeToLocalFileTime(Time,lft);
//转换FILETIME格式成为DOStime格式
FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
//最后,转换DOStime格式成为Delphi's应用的TdateTime格式
Result:=FileDateToDateTime(dft);
end
else
result:=0;
end;