根据 Inno Setup 中的用户首选项编辑已安装的 XML 文件 [英] Edit installed XML file according to user preferences in Inno Setup

查看:49
本文介绍了根据 Inno Setup 中的用户首选项编辑已安装的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I've been struggling with this problem for a few days now. Currently making an installer for our company software but the customer has to be able to fill in a URL that gets saved in the app.exe.config.

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

I've been through a lot of googling and found this piece of code that I edited.

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' 但没有结果,因为安装程序开始抱怨它可以't 找到 XML 文件

and it does what is has to do if I specify an existing path like C:\AutoScan.exe.config but the setup starts complaining if the file doesn't exist. Of course the file only exists after it is installed. but in this case I want the file edited inside the installer I tried it with '{src}\AutoScan.exe.config' and '{app}\AutoScan.exe.config' but without result as the installer starts complaining it can't find the XML file

推荐答案

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

You probably just need to edit the file after the installation completes.

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;


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


Also you should not load the value every time you get to the custom page, because you reset user preference, every time user returns back to the custom page.

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

You should load it only once in the InitializeWizard.

硬编码默认值.

或者如果你真的需要从嵌入的文件中读取它,你必须临时提取它.

Or if you really need to read it from the embedded file, you have to temporarily extract it.

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;

这篇关于根据 Inno Setup 中的用户首选项编辑已安装的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆