windows 如何在 Delphi 中获取文件的创建/上次修改日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/144453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get create/last modified dates of a file in Delphi?
提问by OregonGhost
I want to get a files these attributes as integer values.
我想将这些属性作为整数值获取文件。
回答by Toon Krijthe
Try
尝试
function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean;
From SysUtils.
来自 SysUtils。
回答by OregonGhost
Delphians tend to like the FindFirst approach (the SearchRec structure has some of those), but I'd suggest the Win32 API function GetFileAttributesEx.
Delphians 倾向于喜欢 FindFirst 方法(SearchRec 结构中有一些),但我建议使用 Win32 API 函数 GetFileAttributesEx。
回答by gabr
From the DSiWin32freeware library:
来自DSiWin32免费软件库:
function DSiFileTimeToDateTime(fileTime: TFileTime; var dateTime: TDateTime): boolean;
var
sysTime: TSystemTime;
begin
Result := FileTimeToSystemTime(fileTime, sysTime);
if Result then
dateTime := SystemTimeToDateTime(sysTime);
end; { DSiFileTimeToDateTime }
function DSiGetFileTimes(const fileName: string; var creationTime, lastAccessTime,
lastModificationTime: TDateTime): boolean;
var
fileHandle : cardinal;
fsCreationTime : TFileTime;
fsLastAccessTime : TFileTime;
fsLastModificationTime: TFileTime;
begin
Result := false;
fileHandle := CreateFile(PChar(fileName), GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, 0, 0);
if fileHandle <> INVALID_HANDLE_VALUE then try
Result :=
GetFileTime(fileHandle, @fsCreationTime, @fsLastAccessTime,
@fsLastModificationTime) and
DSiFileTimeToDateTime(fsCreationTime, creationTime) and
DSiFileTimeToDateTime(fsLastAccessTime, lastAccessTime) and
DSiFileTimeToDateTime(fsLastModificationTime, lastModificationTime);
finally
CloseHandle(fileHandle);
end;
end; { DSiGetFileTimes }
回答by JosephStyons
This should work, and it is native Delphi code.
这应该可以工作,而且它是原生的 Delphi 代码。
function GetFileModDate(filename : string) : integer;
var
F : TSearchRec;
begin
FindFirst(filename,faAnyFile,F);
Result := F.Time;
//if you wanted a TDateTime, change the return type and use this line:
//Result := FileDateToDatetime(F.Time);
FindClose(F);
end;
回答by Ian Murphy
function GetFileModDate(filename : string) : TDateTime;
var
F : TSearchRec;
begin
FindFirst(filename,faAnyFile,F);
Result := F.TimeStamp;
//if you really wanted an Int, change the return type and use this line:
//Result := F.Time;
FindClose(F);
end;
F.Time has since been Deprecated, Help file says Use F.TimeStamp.
Just to update this due to later versions of Delphi
F.Time 已被弃用,帮助文件显示使用 F.TimeStamp。
只是为了更新这个由于更高版本的 Delphi
回答by thvedel
System.IOUtils do have a TFile record with several functions for getting file age, e.g. GetCreationTime, GetLastAccessTime, GetLastWriteTime
System.IOUtils 确实有一个 TFile 记录,其中包含多个用于获取文件年龄的函数,例如 GetCreationTime、GetLastAccessTime、GetLastWriteTime
回答by Lars Truijens
You could call the GetFileInformationByHandlewinapi function. Aparently JCLhas a GetFileLastWrite function you could also use
您可以调用GetFileInformationByHandlewinapi 函数。显然JCL有一个 GetFileLastWrite 函数,你也可以使用