根据Inno Setup中选定的组件启动自定义代码

问题描述

我的查询Launch custom code via tasks in Inno Setup有点类似,除了不启动辅助选择页面,而是根据所选组件运行代码变体。我希望将文本的变体插入(设置)文档中。我猜测使用上述参考代码的最初尝试无效,因为inno无法在安装过程的早期搜索文档的存在。我打算在下面使用Append方法。看来Append不支持组件标志。

[Components]
Name: "Adult"; Description: "Adult filters"; Flags: exclusive
Name: "PresetWordFilter"; Description: "Preset Word Filter"; Flags: exclusive
Name: "No_Security"; Description: "No filters"; Flags: exclusive
[Code]
procedure ?
begin
  if ? then
  begin
    FileName := ExpandConstant('{userappdata}\LLL’);
    FileName := AddBackslash(FileName) + 'lll.props';
    Lines := TStringList.Create;

    { Load existing lines from file }
    Lines.LoadFromFile(FileName);
    { Add your information to the end of the file }
    Lines.Append('xxx');
    Lines.Append('FILTER_ADULT=true');
    Lines.SavetoFile(FileName);
    Lines.Free;
  end;
end;

解决方法

使用WizardIsComponentSelected function。例如,在CurStepChanged event function(在ssPostInstallssInstall步骤中)。

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    FileName := ExpandConstant('{userappdata}\LLL\lll.props');
    Lines := TStringList.Create;
    { Load existing lines from file }
    Lines.LoadFromFile(FileName);

    if WizardIsComponentSelected('Adult') then
    begin
      Lines.Append('FILTER_ADULT=true');
    end;

    if WizardIsComponentSelected('PresetWordFilter') then
    begin
      Lines.Append('PRESET_WORD_FILTER=true');
    end;

    Lines.SaveToFile(FileName);
    Lines.Free;    
  end;
end;