【问题标题】:Inno Setup - How to copy a file before setup start?Inno Setup - 如何在安装开始前复制文件?
【发布时间】:2019-10-09 23:09:52
【问题描述】:

我需要在 Inno Setup 启动之前或“选择目录”页面之前将文件复制到一个文件夹。我希望从安装程序而不是从外部源复制此文件。

我正在使用此代码:

function NextButtonClick(PageID: Integer): Boolean;
begin
  Result := True;
  if (PageId = wpWelcome) then
  begin
    FileCopy(
      ExpandConstant('file.exe'),
      ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'), false);
  end;
end;

【问题讨论】:

  • 请注意,在用户单击最终的下一步以实际开始安装之前,通常不赞成对用户的系统进行更改——毕竟,到那时他们可能仍想取消。在 HKCU 中使用或修改设置也是不受欢迎的,因为它们可能不适合您期望的用户。

标签: inno-setup


【解决方案1】:

要随时从安装存档中提取文件,您必须使用ExtractTemporaryFile 过程。此过程将文件从[Files] 部分提取到安装应用程序使用的临时目录,您可以在{tmp} 常量指定的路径上找到该目录。然后,您只需通过扩展上述常量,将提取的文件从那里复制到目标目录。

如果您想在初始化设置时但在创建向导表单之前执行某些操作,请使用InitializeSetup 事件函数。请注意,您甚至可以在不查看向导表单的情况下退出该功能的设置,例如如果您要复制的文件非常重要。这是一个示例代码,但请先查看其中的commented version 以了解一些详细信息:

[Code]
function InitializeSetup: Boolean;
begin
  Result := True;
  ExtractTemporaryFile('File.exe');
  if FileCopy(ExpandConstant('{tmp}\File.exe'), 
    ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\File.exe'), False) 
  then
    MsgBox('File copying succeeded!', mbInformation, MB_OK)
  else
    MsgBox('File copying failed!', mbError, MB_OK)  
end;

【讨论】:

    【解决方案2】:

    您需要先将文件解压缩到一个临时目录,然后将其复制到您想要的位置。像这样的东西应该可以工作:

    ; Script generated by the Inno Setup Script Wizard.
    ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
    
    #define MyAppName "My Program"
    #define MyAppVersion "1.5"
    #define MyAppPublisher "My Company, Inc."
    #define MyAppURL "http://www.example.com/"
    #define MyAppExeName "MyProg.exe"
    
    [Setup]
    ; NOTE: The value of AppId uniquely identifies this application.
    ; Do not use the same AppId value in installers for other applications.
    ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
    AppId={{5820E516-8DD7-4481-A016-63D3F00438C8}
    AppName={#MyAppName}
    AppVersion={#MyAppVersion}
    ;AppVerName={#MyAppName} {#MyAppVersion}
    AppPublisher={#MyAppPublisher}
    AppPublisherURL={#MyAppURL}
    AppSupportURL={#MyAppURL}
    AppUpdatesURL={#MyAppURL}
    DefaultDirName={pf}\{#MyAppName}
    DefaultGroupName={#MyAppName}
    OutputBaseFilename=setup
    Compression=lzma
    SolidCompression=yes
    
    [Languages]
    Name: "english"; MessagesFile: "compiler:Default.isl"
    
    [Tasks]
    Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
    
    [Files]
    Source: "C:\Program Files (x86)\Inno Setup 5\Examples\MyProg.exe"; DestDir: "{app}"; Flags: ignoreversion
    ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
    
    [Icons]
    Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
    Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
    
    [Run]
    Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, "&", "&&")}}"; Flags: nowait postinstall skipifsilent
    
    
    [Code]
    function InitializeSetup: Boolean;
    var
      S: AnsiString;
    begin
      // Show the contents of Readme.txt (non Unicode) in a message box
      log('Before Extract');
      ExtractTemporaryFile('myprog.exe');
      log('Before FileCopy. Dest:' + ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'));
      log('temp: ' + ExpandConstant('{tmp}\myprog.exe'));
      FileCopy(ExpandConstant('{tmp}\myprog.exe'), ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\file.exe'), false);
      log('After FileCopy');
      Result := True;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多