1

我有一个 InnoSetup 项目,我希望能够根据发送到 setup exe 的命令行参数设置 InfoAfterFile。有办法吗?在这种情况下是否有类似“检查”参数的东西?

4

1 回答 1

2

InfoAfterFile您不能在运行时为指令赋值。它的值指定编译到输出设置中的文件,因此必须在编译时知道该指令值。但是,您可以手动将文件加载到InfoAfterMemo控件中。以下示例显示了如何加载由命令行参数指定的文件,-iaf仅当该参数存在且文件存在时:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
InfoAfterFile=c:\DefaultFileToBeDisplayed.txt

[Code]
const
  InfoAfterFileParam = '-iaf';

function TryGetInfoAfterFileParam(var FileName: string): Boolean;
var
  S: string;
  I: Integer;
  Index: Integer;
begin
  // initialize result
  Result := False;
  // iterate all the command line parameters
  for I := 1 to ParamCount do
  begin
    // store the current parameter to the local variable
    S := ParamStr(I);
    // try to find the position of the substring specified by the
    // InfoAfterFileParam constant; in this example we're looking
    // for the "-iaf" string in the current parameter
    Index := Pos(InfoAfterFileParam, S);
    // if the InfoAfterFileParam constant string was found in the
    // current parameter, then...
    if Index = 1 then
    begin
      // strip out the InfoAfterFileParam constant string from the
      // parameter string, so we get only file name
      Delete(S, Index, Length(InfoAfterFileParam));
      // now trim the spaces from rest of the string, which is the
      // file name
      S := Trim(S);
      // check if the file pointed by the file name we got exists;
      // if so, then return True for this function call and assign
      // the output file name to the output parameter
      if FileExists(S) then
      begin
        Result := True;
        FileName := S;
      end;
      // we've found the parameter starting with InfoAfterFileParam
      // constant string, so let's exit the function
      Exit;
    end;
  end;
end;

procedure InitializeWizard;
var
  FileName: string;
begin
  // when the parameter was found and the file to be loaded exists,
  // then load it to the InfoAfterMemo control
  if TryGetInfoAfterFileParam(FileName) then
    WizardForm.InfoAfterMemo.Lines.LoadFromFile(FileName);
end;

请注意,您必须指定InfoAfterFile指令,否则页面将不会显示。另请注意,我正在寻找以 开头的命令行参数-iaf,因此如果您希望使用 eg-iafx参数,则需要实际修改此代码。这是从命令行进行此类设置的示例调用:

Setup.exe -iaf"C:\SomeFolder\SomeFileToBeShownAsInfoAfter.txt"
于 2013-11-05T12:36:31.170 回答