windows - 如何使文件的创建、访问和修改日期与 Windows 属性相同?

标签 windows delphi winapi

我正在尝试获取与 Windows 属性中显示的相同的创建日期、访问日期和修改日期:

File Properties

但我发现时间总是晚 30 分钟:

File Properties Delphi

相信它可能与时区/夏令时有关,但一直无法找到解决方案。试过看: TimeZone Bias 以及调整和查看不同的方法,包括: How to get create/last modified dates of a file in Delphi?

当前代码:

var
MyFd TWin32FindData;
FName: string;
MyTime: TFileTime;
MySysTime: TSystemTime;
myDate, CreateTime, AccessTime, ModTime: TDateTime; 
Begin
 ...
 FindFirstFile(PChar(FName), MyFd);
 MyTime:=MyFd.ftCreationTime;
 FileTimeToSystemTime(MyTime, MySysTime);
 myDate := EncodeDateTime(MySysTime.wYear, MySysTime.wMonth, MySysTime.wDay, MySysTime.wHour,
 MySysTime.wMinute, MySysTime.wSecond, MySysTime.wMilliseconds);
 Memo1.Lines.Add('Created: '+ FormatDateTime('dddd, d mmmm yyyy, hh:mm:ss ampm', MyDate));
 ...

感谢任何帮助

谢谢 保罗

最佳答案

我不确定您当前的代码有什么问题,但我相信这段代码会使用标准的 Windows API 调用来满足您的需求。

procedure TMyForm.ReportFileTimes(const FileName: string);

  procedure ReportTime(const Name: string; const FileTime: TFileTime);
  var
    SystemTime, LocalTime: TSystemTime;
  begin
    if not FileTimeToSystemTime(FileTime, SystemTime) then
      RaiseLastOSError;
    if not SystemTimeToTzSpecificLocalTime(nil, SystemTime, LocalTime) then
      RaiseLastOSError;
    Memo1.Lines.Add(Name + ': ' + DateTimeToStr(SystemTimeToDateTime(LocalTime)));
  end;

var
  fad: TWin32FileAttributeData;

begin
  if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad) then
    RaiseLastOSError;
  Memo1.Clear;
  Memo1.Lines.Add(FileName);
  ReportTime('Created', fad.ftCreationTime);
  ReportTime('Modified', fad.ftLastWriteTime);
  ReportTime('Accessed', fad.ftLastAccessTime);
end;

procedure TMyForm.Button1Click(Sender: TObject);
begin
  ReportFileTimes(Edit1.Text);
end;

关于windows - 如何使文件的创建、访问和修改日期与 Windows 属性相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209394/

相关文章:

c - 如何在共享内存中保存结构 C - Windows

c++ - 在无模式对话框窗口打开时通过加速键

python - 如何让py2exe内置版权信息

python - 需要一个python输入控制程序

python - ctypes:使用 lib.so(在 Mac 上)时指针地址修改,但在使用 lib.dll(在 Windows 上)时不修改

asp.net - 使用 Delphi 和 TIdHttp 将数据发布到 ASP .NET 页面

multithreading - 德尔福TThreadPool : wait for free thread slot before proceeding with code

delphi - 存在哪些库可以在delphi中创建 "moving/living"UI?

windows - 在 Win7 机器上拖放到我的应用程序

c++ - 如何在 C++ 中启动具有管理员权限的应用程序?