【发布时间】:2011-12-09 13:32:57
【问题描述】:
我现在一直在 Google(和此处)上搜索 HOURS。
我找不到解决办法。
我想在 DELPHI 6 中CHANGE“Created Filetime”(= 创建文件时间)。
不是“修改文件时间”(需要简单调用“FileSetDate()”) 而不是“上次访问的文件时间”。
我该怎么做?
【问题讨论】:
我现在一直在 Google(和此处)上搜索 HOURS。
我找不到解决办法。
我想在 DELPHI 6 中CHANGE“Created Filetime”(= 创建文件时间)。
不是“修改文件时间”(需要简单调用“FileSetDate()”) 而不是“上次访问的文件时间”。
我该怎么做?
【问题讨论】:
调用SetFileTime Windows API 函数。如果您只想修改创建时间,请将nil 传递给lpLastAccessTime 和lpLastWriteTime。
您需要通过调用 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 单元中不存在。
【讨论】:
基于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;
【讨论】: