如何在 Inno Setup Pascal 脚本中设置现有文件的创建时间
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
class:
var
FileStream: TFileStream;
begin
{ ... }
FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
try
SetFileTime(FileStream.Handle, CreationTime, LastAccessTime, LastWriteTime);
finally
FileStream .Free;
end;
end;
尽管正如@Ken 所写,在大多数情况下,将 [Files]
部分条目与 external
flag 一起使用会更容易。
当通过 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
class:
var
FileStream: TFileStream;
begin
{ ... }
FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
try
SetFileTime(FileStream.Handle, CreationTime, LastAccessTime, LastWriteTime);
finally
FileStream .Free;
end;
end;
尽管正如@Ken 所写,在大多数情况下,将 [Files]
部分条目与 external
flag 一起使用会更容易。