在 Inno Setup 中将命令行参数值保存到文本文件

Saving command-line parameter value to text file in Inno Setup

除了通常的安装任务外,我还需要获取命令行参数的值(例如/MyParam=XXX),然后将参数值(XXX)复制到一个txt文件中应用程序文件夹。到目前为止,我尝试了下面的代码,但我不知道如何正确地将参数值传递给代码过程。

[Files]
Source: "MyApp.exe"; DestDir: "{app}"; Flags: ignoreversion overwritereadonly; \
    AfterInstall: SaveParam('{param:MyParam}');

[Code]
procedure SaveParam(S: String);
begin
  if S<>'' then
  begin
    SaveStringToFile('{app}\file.txt', 'Value=' + S, False);
  end;
end;

简而言之,当我通过 MyApp.exe /MyParam=XXX 运行 安装程序时,我希望 Value=XXX 文本将添加到 file.txt 文件中。非常感谢任何帮助,谢谢!

从这里开始:Is it possible to accept custom command line parameters with Inno Setup

您需要了解的其他事项:

  • 您必须 ExpandConstant 文件路径中的 {app}
  • 您应该使用CurStepChanged来触发写作。使用不相关文件的 AfterInstall 也可以,但更像是一种 hack。
procedure CurStepChanged(CurStep: TSetupStep);
var
  Value: string;
begin
  if CurStep = ssPostInstall then
  begin
    Value := ExpandConstant('{param:MyParam}');
    if (Value <> '') or CmdLineParamExists('MyParam') then
    begin
      Log('Writing "' + Value + '" to file...');
      SaveStringToFile(
        ExpandConstant('{app}\file.txt'), 'Value=' + Value, False);
    end;
  end;
end;
  • 对于 CmdLineParamExists,请参阅顶部链接的我的回答。
  • 请注意 SaveStringToFile 不是 Unicode 安全的。对于 Unicode 文件,请参阅