Inno Setup - 如何在没有提示消息的情况下保存Xml节点

时间:2013-01-19 21:59:28

标签: xml inno-setup

我希望安装程序在没有提示消息的情况下静默编辑xml文件,如果xml文件存在于目标位置,安装程序会对其进行编辑,如果目标位置不存在,则安装继续并忽略没有提示消息,xml文件不存在。我看到CodeAutomation.iss,但这对我没有帮助。请帮助您提供代码示例。

[Files]
Source: GameConfiguration.xml; DestDir: "{pf}\Game\Sala"; Flags: uninsneveruninstall;

procedure SaveValueToXML(const AFileName, APath, AValue: string);
var
  XMLNode: Variant;
  XMLDocument: Variant;  
begin
  XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XMLDocument.async := False;
    XMLDocument.load(AFileName);
    if (XMLDocument.parseError.errorCode <> 0) then
      MsgBox('Install Garena. ' +
        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('Install Garena', mbError, MB_OK);
  end;
end;

  function NextButtonClick2(PageID: Integer): Boolean;
 begin
    Result := True;
    if (PageId = wpFinished) then begin
        SaveValueToXML(ExpandConstant('{pf}\Game\Sala\GameConfiguration.xml'), '//@param', PEdit.Text);
        SaveValueToXML(ExpandConstant('{pf}\Game\Sala\GameConfiguration.xml'), '//@path', ExpandConstant('{reg:HKCU\SOFTWARE\xxx,InstallPath}\xxx.exe'));
    end;
 end;

1 个答案:

答案 0 :(得分:4)

首先测试XML文件是否存在:

function NextButtonClick2(PageID: Integer): Boolean;
var
  XMLFile: string;
begin
  Result := True;
  if (PageId = wpFinished) then 
  begin
    XMLFile := ExpandConstant('{pf}\Game\Sala\GameConfiguration.xml');
    if FileExists(XMLFile) then 
    begin
      SaveValueToXML(XMLFile, '//@param', PEdit.Text);
      SaveValueToXML(XMLFile, '//@path', 
        ExpandConstant('{reg:HKCU\SOFTWARE\XXX,InstallPath}\xxx.exe'));
    end;
  end;
end;

端;