如何修改一个文件的最后更改时间(32分)

  • 主题发起人 主题发起人 我爱PASCAL
  • 开始时间 开始时间

我爱PASCAL

Unregistered / Unconfirmed
GUEST, unregistred user!
fileage只能得到文件更改时间,用什么函数能够修改呢
 
var<br>&nbsp; f: file;<br>begin<br>&nbsp; Assign(f, '文件名');<br>&nbsp; Reset(f);<br>&nbsp; SetFTime(f, Time);<br>&nbsp; Close(f);<br>end;
 
不知道这种方法效率高不高,要打开文件。
 
Function FtToDt(AFT:_FILETIME):TDateTime;<br>Var<br>&nbsp; LocalFT : _FILETIME;<br>&nbsp; SysT &nbsp; &nbsp;: _SystemTime;<br>begin<br>&nbsp; FileTimeToLocalFileTime(AFT, LocalFT);<br>&nbsp; FileTimeToSystemTime(LocalFT, SysT);<br>&nbsp; Result := SystemTimeToDateTime(SysT);<br>end;<br><br>Function DtToFt(ADT:TDateTime):_FILETIME;<br>Var<br>&nbsp; SysT &nbsp; &nbsp;: _SystemTime;<br>&nbsp; LocalFT : _FILETIME;<br>begin<br>&nbsp; DateTimeToSystemTime(ADT, SysT);<br>&nbsp; SystemTimeToFileTime(SysT, LocalFT);<br>&nbsp; LocalFileTimeToFileTime(LocalFT, Result);<br>end;<br><br>Function ReadFileLWTime(AFile:String):TDateTime;<br>var<br>&nbsp; hFileOld :THandle;<br>&nbsp; LastWriteTime :_FileTime;<br>begin<br>&nbsp; Result := 0;<br><br>&nbsp; hFileOld :=CreateFile(Pchar(AFile),generic_read,file_share_read,nil, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open_existing,FILE_ATTRIBUTE_NORMAL,Cardinal(nil));<br>&nbsp; if (hFileOld=INVALID_HANDLE_VALUE) then exit;<br><br>&nbsp; GetFileTime(hFileOld,nil,nil,@LastWriteTime);<br><br>&nbsp; CloseHandle(hFileOld);<br><br>&nbsp; Result := FtToDt(LastWriteTime);<br>end;<br><br>procedure SetFileLWTime(AFile:String; ADateTime:TDateTime);<br>Var<br>&nbsp; hFileNew :THandle;<br>&nbsp; LastWriteTime :_FileTime;<br>begin<br>&nbsp; hFileNew :=createFile(PChar(AFile),generic_write,file_share_write,nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open_existing,FILE_ATTRIBUTE_NORMAL,Cardinal(nil));<br>&nbsp; if (hFileNew=INVALID_HANDLE_VALUE) then exit;<br><br>&nbsp; LastWriteTime := DtToFt(ADateTime);<br><br>&nbsp; SetFileTime(hFileNew,nil,nil,@LastWriteTime);<br><br>&nbsp; CloseHandle(hFileNew);<br>end;
 
由于我的文件已经打开,并不需要转换成DATETIME,<br>我就用了<br>GetFileTime(hFileOld,nil,nil,@LastWriteTime);<br>SetFileTime(hFileNew,nil,nil,@LastWriteTime);<br>这两个函数。转换的部份我就省了。
 
多人接受答案了。
 
后退
顶部