如何不修改文件的任何属性来拷贝文件。 ( 积分: 20 )

  • 主题发起人 主题发起人 xf47
  • 开始时间 开始时间
X

xf47

Unregistered / Unconfirmed
GUEST, unregistred user!
我想不修改一个文件的任何属性进行文件拷贝(包括创建时间、修改时间,最后访问时间等),请问用什么方法呢?有代码更好。谢谢
 
我想不修改一个文件的任何属性进行文件拷贝(包括创建时间、修改时间,最后访问时间等),请问用什么方法呢?有代码更好。谢谢
 
用正常的文件复制,复制完成后再用API:SetFileTime修改文件的属性<br><br>引用以前的帖子:<br>type<br> &nbsp;TFileTimeType = (fttCreation, fttLastAccess, fttLastWrite);<br> &nbsp;//分别对应文件创建时间,访问时间,修改时间<br>function GetFileDateTime(const FileName: string; FileTimeType: TFileTimeType): TDateTime;<br>var<br> &nbsp;Handle: THandle;<br> &nbsp;FindData: TWin32FindData;<br> &nbsp;LocalFileTime: TFileTime;<br> &nbsp;DosDateTime: Integer;<br>begin<br> &nbsp;Handle := FindFirstFile(PChar(FileName), FindData);<br> &nbsp;if Handle &lt;&gt; INVALID_HANDLE_VALUE then<br> &nbsp;begin<br> &nbsp; &nbsp;Windows.FindClose(Handle);<br> &nbsp; &nbsp;if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;case FileTimeType of<br> &nbsp; &nbsp; &nbsp;fttCreation:<br> &nbsp; &nbsp; &nbsp; &nbsp;FileTimeToLocalFileTime(FindData.ftCreationTime, LocalFileTime);<br> &nbsp; &nbsp; &nbsp;fttLastAccess:<br> &nbsp; &nbsp; &nbsp; &nbsp;FileTimeToLocalFileTime(FindData.ftLastAccessTime, LocalFileTime);<br> &nbsp; &nbsp; &nbsp;fttLastWrite:<br> &nbsp; &nbsp; &nbsp; &nbsp;FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if FileTimeToDosDateTime(LocalFileTime, LongRec(DosDateTime).Hi,<br> &nbsp; &nbsp; &nbsp; &nbsp;LongRec(DosDateTime).Lo) then <br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := FileDateToDateTime(DosDateTime);<br> &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;Result := -1;<br>end;<br><br>function SetFileDateTime(const FileName: string; FileTimeType: TFileTimeType; DateTime: TDateTime): Integer;<br>var<br> &nbsp;Handle: THandle;<br> &nbsp;LocalFileTime, FileTime: TFileTime;<br> &nbsp;DosDateTime: Integer;<br> &nbsp;I : TFileTimeType;<br> &nbsp;FileTimes: array[TFileTimeType] of Pointer;<br>begin<br> &nbsp;Result := 0;<br> &nbsp;DosDateTime := DateTimeToFileDate(DateTime);<br> &nbsp;Handle := FileOpen(FileName, fmOpenWrite or fmShareDenyNone);<br> &nbsp;if Handle &lt;&gt; INVALID_HANDLE_VALUE then<br> &nbsp;try<br> &nbsp; &nbsp;for I := fttCreation to fttLastWrite do<br> &nbsp; &nbsp; &nbsp;FileTimes := nil;<br> &nbsp; &nbsp;DosDateTimeToFileTime(LongRec(DosDateTime).Hi, LongRec(DosDateTime).Lo, LocalFileTime);<br> &nbsp; &nbsp;LocalFileTimeToFileTime(LocalFileTime, FileTime);<br> &nbsp; &nbsp;FileTimes[FileTimeType] := @FileTime;<br> &nbsp; &nbsp;if SetFileTime(Handle, FileTimes[fttCreation], FileTimes[fttLastAccess],<br> &nbsp; &nbsp; &nbsp;FileTimes[fttLastWrite]) then Exit;<br> &nbsp;finally<br> &nbsp; &nbsp;FileClose(Handle);<br> &nbsp;end;<br> &nbsp;Result := GetLastError;<br>end;<br>使用举例:<br>1、获取文件创建时间:<br> &nbsp; ShowMessage(DateTimeToStr(GetFileDateTime('c:/key.txt',fttLastWrite)));<br>2、设置文件修改时间:<br> &nbsp; SetFileDateTime('c:/key.txt',fttLastWrite, StrToDateTime('2000/01/01'));
 
接受答案了.
 

Similar threads

D
回复
0
查看
814
DelphiTeacher的专栏
D
D
回复
0
查看
760
DelphiTeacher的专栏
D
D
回复
0
查看
708
DelphiTeacher的专栏
D
后退
顶部