根据 Inno Setup 中的用户首选项编辑已安装的 XML 文件

Edit installed XML file according to user preferences in Inno Setup

所以这几天我一直在为这个问题苦苦挣扎。 目前正在为我们公司的软件制作安装程序,但客户必须能够填写 URL 并保存在 app.exe.config.

我通过大量谷歌搜索找到了我编辑的这段代码。

var
  CustomEdit: TEdit;
  CustomPageID: Integer;

function LoadValueFromXML(const AFileName, APath: string): string;
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  Result := '';
  XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      Result := XMLNode.text;
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

procedure SaveValueToXML(const AFileName, APath, AValue: string);
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('The XML file could not be parsed. ' + 
        XMLDocument.parseError.reason, mbError, MB_OK)
    else
    begin
      XMLDocument.setProperty('SelectionLanguage', 'XPath');
      XMLNode := XMLDocument.selectSingleNode(APath);
      XMLNode.text := AValue;
      XMLDocument.save(AFileName);
    end;
  except
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);
  end;
end;

procedure InitializeWizard;
var  
  CustomPage: TWizardPage;
begin
  CustomPage := CreateCustomPage(wpWelcome, 'Custom Page', 
    'Enter the new value that will be saved into the XML file');
  CustomPageID := CustomPage.ID;
  CustomEdit := TEdit.Create(WizardForm);
  CustomEdit.Parent := CustomPage.Surface;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = CustomPageID then
  begin
    CustomEdit.Text :=
      LoadValueFromXML('C:\AutoScan.exe.config',
        '//configuration/system.serviceModel/client/endpoint/address');
  end;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;
  if CurPageID = CustomPageID then
  begin
    SaveValueToXML(
      'C:\AutoScan.exe.config',
      '//configuration/system.serviceModel/client/endpoint/address',
      CustomEdit.Text);
  end;
end; 

如果我指定一个现有路径(如 C:\AutoScan.exe.config),它会执行必须执行的操作,但如果文件不存在,安装程序就会开始抱怨。 当然文件只有在安装后才存在。但在这种情况下,我希望在安装程序中编辑文件我用'{src}\AutoScan.exe.config'和'{app}\AutoScan.exe.config'尝试过但没有结果,因为安装程序开始抱怨它不能'找不到 XML 文件

您可能只需要在安装完成后编辑文件。

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    SaveValueToXML(
      'C:\AutoScan.exe.config',
      '//configuration/system.serviceModel/client/endpoint/address',
      CustomEdit.Text);
  end;
end;

此外,您不应在每次进入自定义页面时都加载该值,因为每次用户 returns 返回自定义页面时都会重置用户首选项。

您应该只在 InitializeWizard 中加载一次。

要么硬编码默认值。

或者确实需要从嵌入文件中读取,就得临时解压了。

procedure InitializeWizard;
var  
  CustomPage: TWizardPage;
begin
  CustomPage :=
    CreateCustomPage(
      wpWelcome, 'Custom Page',
      'Enter the new value that will be saved into the XML file');
  CustomEdit := TEdit.Create(WizardForm);
  CustomEdit.Parent := CustomPage.Surface;
  ExtractTemporaryFile('AutoScan.exe.config');
  CustomEdit.Text :=
    LoadValueFromXML(
      ExpandConstant('{tmp}\AutoScan.exe.config'),
      '//configuration/system.serviceModel/client/endpoint/address');
end;