如何在Delphi中获取文件的创建/上次修改日期?

时间:2023-01-26 16:17:07

I want to get a files these attributes as integer values.

我想将这些属性的文件作为整数值。

7 个解决方案

#1


12  

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。

#2


13  

Try

尝试

function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean;

From SysUtils.

来自SysUtils。

#3


6  

From the DSiWin32 freeware 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 }

#4


4  

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;

#5


3  

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

#6


0  

You could call the GetFileInformationByHandle winapi function. Aparently JCL has a GetFileLastWrite function you could also use

你可以调用GetFileInformationByHandle winapi函数。显然JCL有一个你也可以使用的GetFileLastWrite函数

#7


0  

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

#1


12  

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。

#2


13  

Try

尝试

function FileAge(const FileName: string; out FileDateTime: TDateTime): Boolean;

From SysUtils.

来自SysUtils。

#3


6  

From the DSiWin32 freeware 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 }

#4


4  

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;

#5


3  

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

#6


0  

You could call the GetFileInformationByHandle winapi function. Aparently JCL has a GetFileLastWrite function you could also use

你可以调用GetFileInformationByHandle winapi函数。显然JCL有一个你也可以使用的GetFileLastWrite函数

#7


0  

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