如何编程改变文件的创建时间和修改时间(200分)

  • 主题发起人 主题发起人 microbit
  • 开始时间 开始时间
M

microbit

Unregistered / Unconfirmed
GUEST, unregistred user!
如何编程改变文件的创建时间和修改时间
 
一个笨办法:
改创建时间:在程序里面copy目标文件,把源文件删除,再把copy的副本改回原来的名字
改修改时间:以读写方式打开目标文件,读出其中一个字节,再写回原位
两者都改:把目标文件打开,读出所有内容(希望他不要太大^-^),删掉源文件,重新
; ; ; ; ; 创建一个,把内容全部写回去。
 
file management routines

function FileSetDate(Handle: Integer; Age: Integer): Integer;

Description

FileSetDate sets the OS timestamp of the file given by Handle to the value given by Age. The DateTimeToFileDate function can be used to convert a TDateTime value to a OS timestamp.

The return value is zero if the function was successful. Otherwise the return value is an error code.
 
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;

Description

Call TimeStampToDateTime to convert a TTimeStamp value into a TDateTime value. TTimeStamp values represent time as separate date and time values, where the date is the number of calendar days since the start of the current calendar (that is, January 1, 0001 would have a value of 1), and time is the number of milliseconds since midnight. TDateTime values represent time as the number of days (including fractional days) that have elapsed since 12:00 am on December 30, 1899.
 
以前的帖子:
type
; TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);
; //分别对应文件创建时间,访问时间,修改时间
function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): TDateTime;
var
; Handle: THandle;
; FindData: TWin32FindData;
; LocalFileTime: TFileTime;
; DosDateTime: Integer;
begin
; Handle := FindFirstFile(PChar(FileName), FindData);
; if Handle <> INVALID_HANDLE_VALUE then
; begin
; ; Windows.FindClose(Handle);
; ; if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
; ; begin
; ; ; case FileTimeType of
; ; ; fttCreation:
; ; ; ; FileTimeToLocalFileTime(FindData.ftCreationTime, LocalFileTime);
; ; ; fttLastAccess:
; ; ; ; FileTimeToLocalFileTime(FindData.ftLastAccessTime, LocalFileTime);
; ; ; fttLastWrite:
; ; ; ; FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
; ; ; end;
; ; ; if FileTimeToDosDateTime(LocalFileTime, LongRec(DosDateTime).Hi,
; ; ; ; LongRec(DosDateTime).Lo) then
; ; ; begin
; ; ; ; Result := FileDateToDateTime(DosDateTime);
; ; ; ; Exit;
; ; ; end;
; ; end;
; end;
; Result := -1;
end;

function SetFileDateTime(const FileName: string; FileTimeType: TFileTimeType; DateTime: TDateTime): Integer;
var
; Handle: THandle;
; LocalFileTime, FileTime: TFileTime;
; DosDateTime: Integer;
; I : TFileTimeType;
; FileTimes: array[TFileTimeType] of Pointer;
begin
; Result := 0;
; DosDateTime := DateTimeToFileDate(DateTime);
; Handle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);
; if Handle <> INVALID_HANDLE_VALUE then
; try
; ; for I := fttCreation to fttLastWrite do
; ; ; FileTimes := nil;
; ; DosDateTimeToFileTime(LongRec(DosDateTime).Hi, LongRec(DosDateTime).Lo, LocalFileTime);
; ; LocalFileTimeToFileTime(LocalFileTime, FileTime);
; ; FileTimes[FileTimeType] := @FileTime;
; ; if SetFileTime(Handle, FileTimes[fttCreation], FileTimes[fttLastAccess],
; ; ; FileTimes[fttLastWrite]) then Exit;
; finally
; ; FileClose(Handle);
; end;
; Result := GetLastError;
end;
使用举例:
1、获取文件创建时间:
; ;ShowMessage(DateTimeToStr(GetFileDateTime('c:/key.txt',fttLastWrite)));
2、设置文件修改时间:
; ;SetFileDateTime('c:/key.txt',fttLastWrite, StrToDateTime('2000/01/01'));
 
多人接受答案了。
 
后退
顶部