简体   繁体   English

如何在 Inno Setup Pascal Script 中设置现有文件的创建时间

[英]How to set creation time of existing file in Inno Setup Pascal Script

When copying a file by FileCopy (or also RenameFile ) from a directory to another one an original creation time changes to the current date.通过FileCopy (或RenameFile )将文件从一个目录复制到另一个目录时,原始创建时间更改为当前日期。 I would like to set the creation time to the original one.我想将创建时间设置为原始时间。

I can get the original time values by FindFirst , but how to get a handle of a file to use when calling SetFileTime ?我可以通过FindFirst获取原始时间值,但是如何在调用SetFileTime时获取要使用的文件句柄?

In the [Code] section of Inno Setup, I have this code:在 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

EDIT:编辑:

After the answer of Martin I have modified the code as follow ( sorry if is far than perfect... I am a VB.NET programmer, not a Pascal programmer):在 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;  

To obtain a handle of a file, you can use TFileStream class :要获取文件句柄,可以使用TFileStream

var
  FileStream: TFileStream;
begin
  { ... }
  FileStream := TFileStream.Create(FileName, fmOpenReadWrite);
  try
    SetFileTime(FileStream.Handle, CreationTime, LastAccessTime, LastWriteTime);
  finally
    FileStream .Free;
  end;
end;

Though as @Ken wrote, in most cases, it would be easier to use [Files] section entry with an external flag .尽管正如@Ken 所写,在大多数情况下,使用带有external标志的[Files]部分条目会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM