如何改变文件日期?(100分)

  • 主题发起人 主题发起人 邹国栋
  • 开始时间 开始时间

邹国栋

Unregistered / Unconfirmed
GUEST, unregistred user!
请看下面这段程序:<br>procedure ChangeFileTime(const SourceFileName,ObjectFileName:String);<br>var<br>&nbsp; CreationTime:PFileTime;<br>&nbsp; LastAccessTime:PFileTime;<br>&nbsp; LastWriteTime:PFileTime;<br>&nbsp; SFileName:Cardinal;<br>&nbsp; OFileName:Cardinal;<br>begin<br>SFileName:=Cardinal(SourceFileName);<br>GetFileTime(SFileName,CreationTime,LastAccessTime,LastWriteTime);<br>OFileName:=Cardinal(ObjectFileName);<br>SetFileTime(OFileName,CreationTime,LastAccessTime,LastWriteTime);<br>end;<br>我写的这个过程的目的是将原文件的创建,修改,访问的时间赋予目标文件,但程序执行后目标文件并无任何变化,我不知道这段程序错在哪?请各位高手指。
 
sfilename:=createfile(pchar(sourcefilename),general_read,file_share_read,nil,open_existing,FILE_ATTRIBUTE_NORMAL,nil,nil);<br>GetFileTime(SFileName,CreationTime,LastAccessTime,LastWriteTime);<br>ofilename:=createfile(pchar(objectfilename),general_write,file_share_write,nil,open_existing,FILE_ATTRIBUTE_NORMAL,nil,nil);<br>setFileTime(OFileName,CreationTime,LastAccessTime,LastWriteTime);<br>closehandle(sfilename);<br>closehandle(ofilename);
 
你的setfiletime一定是返回0,执行没有成功!
 
cytown:你的代码也不一定会使setfiletime返回真,我刚才也是象你的代码一样试了<br>一下,还是返回FALSE,不过一定是应该用createfile打开文件了。
 
procedure TForm1.Button1Click(Sender: TObject);<br>const<br>&nbsp; sourcefilename:string='c:/a.txt';<br>&nbsp; objectfilename:string='c:/b.txt';<br>var<br>&nbsp; CreationTime:PFileTime;<br>&nbsp; LastAccessTime:PFileTime;<br>&nbsp; LastWriteTime:PFileTime;<br>&nbsp; SFileName:Thandle;<br>&nbsp; OFileName:Thandle;<br>begin<br>&nbsp; Getmem(CreationTime,SizeOf(TFileTime));<br>&nbsp; Getmem(LastAccesstime,SizeOf(TFileTime));<br>&nbsp; GetMem(LastWriteTime,SizeOf(TFileTime));<br>&nbsp; sfilename:=createfile(pchar(sourcefilename),generic_read,file_share_read,nil,open_existing,FILE_ATTRIBUTE_NORMAL,Cardinal(nil));<br>&nbsp; GetFileTime(SFileName,CreationTime,LastAccessTime,LastWriteTime);<br>&nbsp; ofilename:=createfile(pchar(objectfilename),generic_write,file_share_write,nil,open_existing,FILE_ATTRIBUTE_NORMAL,Cardinal(nil));<br>&nbsp; setFileTime(OFileName,CreationTime,LastAccessTime,LastWriteTime);<br>&nbsp; freemem(CreationTime);<br>&nbsp; freemem(LastAccesstime);<br>&nbsp; freeMem(LastWriteTime);<br>&nbsp; closehandle(sfilename);<br>&nbsp; closehandle(ofilename);<br>end;<br>
 
多人接受答案了。
 
后退
顶部