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

时间:2016-09-13 09:50:56

标签: xml config inno-setup

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

我经历了很多谷歌搜索,发现了我编辑的这段代码。

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
    CustomEdit.Text := LoadValueFromXML('C:\AutoScan.exe.config', '//configuration/system.serviceModel/client/endpoint/address');
end;

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

如果我指定像C:\AutoScan.exe.config这样的现有路径,它会执行以下操作但是如果文件不存在则安装程序会开始抱怨。 当然,该文件仅在安装后才存在。但在这种情况下,我希望在安装程序中编辑的文件我尝试使用'{src} \ AutoScan.exe.config'和'{app} \ AutoScan.exe.config'但没有结果,因为安装程序开始抱怨它可以'找到XML文件

1 个答案:

答案 0 :(得分:0)

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

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;

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

您只应在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;