FileSetDate (Delphi Function) & SetFileTime (Windows API)(44分)

  • 主题发起人 主题发起人 3h
  • 开始时间 开始时间
3

3h

Unregistered / Unconfirmed
GUEST, unregistred user!
请教关于此两函数之用法。<br>我有程序:<br>procedure TMainForm.Btn1Click(Sender: TObject);<br>Var<br>&nbsp; &nbsp;Q : Integer;<br>&nbsp; &nbsp;ST : TSystemTime;<br>&nbsp; &nbsp;FT : TFileTime;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wMonth := EditMon.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wDayOfWeek := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wDay := EditDay.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wYear := EditYear.Value + 2000;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wHour := EditHour.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wMinute := EditMin.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wSecond := EditSec.Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; ST.wMilliseconds := 0;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; SystemTimeToFileTime(ST, FT);<br>&nbsp; &nbsp; &nbsp; &nbsp; Q := FileOpen(Edit1.Text, GENERIC_WRITE);<br>&nbsp; &nbsp; &nbsp; &nbsp; if Q &lt;= 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Showmessage( '文件:' + Edit1.Text +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#10#13 +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'打开时失败,可能文件正在被 Windows 独占禁止共享。')<br>&nbsp; &nbsp; &nbsp; &nbsp; Else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;If SetFileTime(Q, @FT, @FT, @FT) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Showmessage( '文件:' + Edit1.Text +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#10#13 +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'设置日期、时间时失败,可能文件正在被使用,' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#10#13 + '或者被 Windows 独占禁止共享。');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileClose(Q);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;Screen.Cursor := crDefault;<br>end;<br><br>为了不影响大家的集中力,这段东西有所删减,但主线是在的,先打开<br>文件,取得句柄Q,再通过API函数SetFileTime(Q, @FT, @FT, @FT)<br>设置文件的时间,结果是不行,不论怎么样也无法修改文件的日期<br>和时间,将上面一句改为<br>SetFileTime(Q, nil, nil, @FT) 也不行,甚至<br>FileSetDate(Q,0)都不行!<br>请大侠们教我。<br><br>另外请教如果是文件夹应如何处理?
 
If SetFileTime(Q, @FT, @FT, @FT) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Showmessage( ......<br>?????<br>应该是<br>if not SetFileTime(......)吧?
 
对,我在删节的时候搞错了,<br>但是不论有没有NOT都是不行的。
 
SystemTimeToFileTime(ST, @FT);
 
这么写没问题:<br>Var &nbsp; Q : Integer;<br>ST : TSystemTime;<br>FT : TFileTime;<br>begin<br><br>&nbsp; ST.wMonth := 12;<br>&nbsp; ST.wDayOfWeek := 0;<br>&nbsp; ST.wDay := 23;<br>&nbsp; ST.wYear := 1+2000;<br>&nbsp; ST.wHour := 1;<br>&nbsp; ST.wMinute := 1;<br>&nbsp; ST.wSecond := 1;<br>&nbsp; ST.wMilliseconds := 0;<br>&nbsp; SystemTimeToFileTime(ST, FT);<br>&nbsp; Q := FileOpen('e:/test.aaa', fmOpenWrite or fmShareDenyNone); &nbsp; &nbsp; &nbsp; &nbsp;;<br>&nbsp; SetFileTime(Q, @FT, @FT, @FT) ;<br>&nbsp; FileClose(Q);<br>end;
 
是的,<br>delphi fan2的方法可行,可能是打开方式<br>&nbsp; &nbsp; fmOpenWrite or fmShareDenyNone<br>的原因吧。<br><br>不过问题还没有结束,如果是文件夹,显然用FileOpen()无法打开,<br>也就不能取得句柄了,那要如何更改它的时间和日期呢?
 
总的来说就是取得目录的句柄,应如何取得?
 
&nbsp; &nbsp;可用TSearchRec;<br>var<br>&nbsp; &nbsp; DirRec:Tsearchrec;<br>begin<br>&nbsp; &nbsp; FindFirst('c:/Windows',faDirectory,DirRec);<br>&nbsp; &nbsp; if DirRec.Attr = $00000010 &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp;Caption:='directory' + IntToStr(DirRec.FindHandle);<br>&nbsp; &nbsp; &nbsp;...
 
这样取得的句柄只是查找句柄,不能修改目录日期时间的吧?!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
757
import
I
I
回复
0
查看
770
import
I
后退
顶部