10

我现在一直在谷歌(和这里)上搜索 HOURS。

我找不到解决方案。

我想在DELPHI 6中更改创建的文件时间”(= 创建文件时间)。

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

我该怎么做呢?

我的意思的图片...

4

2 回答 2

8

调用SetFileTimeWindows API 函数。如果您只想修改创建时间,请通过nillpLastAccessTimelpLastWriteTime

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

通过将 API 调用包装在一个接收文件名和TDateTime. 此函数应管理获取和关闭文件句柄以及TDateTimeFILETIME.

我会这样做:

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 单元中不存在。

于 2011-12-09T13:44:10.053 回答
7

基于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;
于 2011-12-09T13:41:20.653 回答