【问题标题】:Delphi 6: How can I change created filedate (= file creation date)Delphi 6:如何更改创建的文件日期(= 文件创建日期)
【发布时间】:2011-12-09 13:32:57
【问题描述】:

我现在一直在 Google(和此处)上搜索 HOURS。

我找不到解决办法。

我想在 DELPHI 6CHANGECreated Filetime”(= 创建文件时间)。

不是“修改文件时间”(需要简单调用“FileSetDate()”) 而不是“上次访问的文件时间”。

我该怎么做?

【问题讨论】:

    标签: file delphi date delphi-6


    【解决方案1】:

    调用SetFileTime Windows API 函数。如果您只想修改创建时间,请将nil 传递给lpLastAccessTimelpLastWriteTime

    您需要通过调用 CreateFile 或 Delphi 包装器之一来获取文件句柄,因此这不是最方便使用的 API。

    通过将 API 调用包装在一个接收文件名和 TDateTime 的辅助函数中,让您的生活更轻松。此函数应管理获取和关闭文件句柄以及将TDateTime 转换为FILETIME 的低级细节。

    我会这样做:

    const
      FILE_WRITE_ATTRIBUTES = $0100;
    
    procedure SetFileCreationTime(const FileName: string; const DateTime: TDateTime);
    var
      Handle: THandle;
      SystemTime: TSystemTime;
      FileTime: TFileTime;
    begin
      Handle := CreateFile(PChar(FileName), FILE_WRITE_ATTRIBUTES,
        FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, 0);
      if Handle=INVALID_HANDLE_VALUE then
        RaiseLastOSError;
      try
        DateTimeToSystemTime(DateTime, SystemTime);
        if not SystemTimeToFileTime(SystemTime, FileTime) then
          RaiseLastOSError;
        if not SetFileTime(Handle, @FileTime, nil, nil) then
          RaiseLastOSError;
      finally
        CloseHandle(Handle);
      end;
    end;
    

    我必须添加 FILE_WRITE_ATTRIBUTES 的声明,因为它在 Delphi 6 Windows 单元中不存在。

    【讨论】:

      【解决方案2】:

      基于FileSetDate,可以编写类似的例程:

      function FileSetCreatedDate(Handle: Integer; Age: Integer): Integer;
      var
        LocalFileTime, FileTime: TFileTime;
      begin
        Result := 0;
        if DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime) and
          LocalFileTimeToFileTime(LocalFileTime, FileTime) and
          SetFileTime(Handle, @FileTime, nil, nil) then Exit;
        Result := GetLastError;
      end;
      

      【讨论】:

        猜你喜欢
        • 2010-10-27
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        相关资源
        最近更新 更多