【发布时间】:2018-03-14 01:03:58
【问题描述】:
当通过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;
【问题讨论】:
-
您为什么要手动复制文件而不是让 IS 为您完成?它可以直接这样做,包括在进程中使用标志
Touch设置文件戳。如果你从同一个分区复制,你甚至不必这样做;只需使用external标志。 -
我同意!请通过勾选它们来接受答案。不仅仅是投票。我们免费为我们的答案工作。通过打勾,您正在建立我们的声誉。请查看您个人资料中的问题部分,并在您可以回答的问题上打勾。谢谢。
-
@Martin/Andrew:ops,对不起,我会接受(我想是点击耙子)
-
@Ken:因为文件已经存在,所以它们不是存储在 [File] 部分中的文件。在以前的设置中创建的旧文件,我必须在其他目录中移动以保持创建时间。
-
然后你使用外部标志,正如我之前所说的。它从外部位置(AKA,不在安装文件中)复制文件。