[英]How to set creation time of existing file in Inno Setup Pascal Script
通過FileCopy
(或RenameFile
)將文件從一個目錄復制到另一個目錄時,原始創建時間更改為當前日期。 我想將創建時間設置為原始時間。
我可以通過FindFirst
獲取原始時間值,但是如何在調用SetFileTime
時獲取要使用的文件句柄?
在 Inno Setup 的[Code]
部分,我有以下代碼:
If FileCopy(F1, F2,False) then
If FindFirst(F1,FindRec) then
Try
Fhandle := ??????????? (FindRec.FindHandle don't works)
SetFileTime(
Fhandle, FindRec.CreationTime, FindRec.LastAccessTime, FindRec.LastWriteTime)
finally
FindClose(FindRec);
end
編輯:
在 Martin 的回答之后,我修改了代碼如下(對不起,如果遠非完美......我是 VB.NET 程序員,而不是 Pascal 程序員):
{ C1 and C2 are full Paths }
if Not FileCopy(C1, C2, False) then
begin
MsgBox('Data reading error 01. Setup will be aborted.', mbError, MB_OK);
Result := false;
exit;
end;
if FindFirst(C2, FindRec) then
try
begin
MyTime := FindRec.LastWriteTime //remains the original one
end;
finally
FindClose(FindRec);
end
else
begin
MsgBox('Data reading error 02. Setup will be aborted.', mbError, MB_OK);
Result := false;
exit;
end;
end;
FileStream := TFileStream.Create(C2, fmOpenReadWrite);
Try
if not SetFileTime(FileStream.Handle, MyTime, MyTime, MyTime) Then
begin
MsgBox('Data reading error 03. Setup will be aborted.', mbError, MB_OK);
Result := false;
exit;
end;
Finally
FileStream.Free;
end;
要獲取文件句柄,可以使用TFileStream
類:
var
FileStream: TFileStream;
begin
{ ... }
FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
try
SetFileTime(FileStream.Handle, CreationTime, LastAccessTime, LastWriteTime);
finally
FileStream .Free;
end;
end;
盡管正如@Ken 所寫,在大多數情況下,使用帶有external
標志的[Files]
部分條目會更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.