如何从Innosetup脚本修改exe.config

thi*_*-Me 5 inno-setup pascalscript

我自己开始学习Innosetup脚本.为此我创建了一个简单的C#控制台应用程序,它从配置文件中读取一个元素并输出到控制台.

<configuration>
  <appSettings>
    <add key ="Name" value="Brad Pitt"/> 
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

例如:它应通过查询键属性"Name"来读取值.

我希望从Innosetup安装脚本中写入.config中的值.

即在安装过程中我将收集名称(在这种情况下为"Brad Pitt")并将其写入配置文件的值

<add key ="Name" value="Brad Pitt"/> 
Run Code Online (Sandbox Code Playgroud)

问题是如何使用Pascal脚本或标准脚本实现此目的.

非常感谢任何指导

问候

跋蹉

thi*_*-Me 7

为此,我创建了一个简单的过程,它将xml文件名作为输入.该过程应解析每一行并将内容写入临时文件.代码检查每一行,查找字符串'key ="Name"':

   if (Pos('key="Name"', strTest) <> 0 ) 
Run Code Online (Sandbox Code Playgroud)

如果找到匹配,那么我用我想要的标签替换该特定行,该标签value来自我的自定义页面.

   strTest := '  <add key="Name" value="' + strName + '"/> ';
Run Code Online (Sandbox Code Playgroud)

这会写入临时文件.然后我删除原始的exe.config文件并将临时配置文件重命名为exe.config文件(从而反映了我需要的更改).下面是该过程的完整代码片段,不要忘记从[Files]即调用该过程

[Files]
Source: "HUS.exe.config"; DestDir: "{app}"; AfterInstall: ConvertConfig('HUS.exe.config')
Run Code Online (Sandbox Code Playgroud)

代码片段

procedure ConvertConfig(xmlFileName: String);
var
  xmlFile: String;
  xmlInhalt: TArrayOfString;
  strName: String;
  strTest: String;
  tmpConfigFile: String;
  k: Integer;
begin
  xmlFile := ExpandConstant('{app}') + '\' + xmlFileName;
  tmpConfigFile:= ExpandConstant('{app}') + '\config.tmp';
  strName :=  UserPage.Values[0] +' '+ UserPage.Values[1];

  if (FileExists(xmlFile)) then begin
    // Load the file to a String array
    LoadStringsFromFile(xmlFile, xmlInhalt);

    for k:=0 to GetArrayLength(xmlInhalt)-1 do begin
      strTest := xmlInhalt[k];
      if (Pos('key="Name"', strTest) <> 0 ) then  begin
        strTest := '  <add key="Name" value="' + strName + '"/> ';
      end;
      SaveStringToFile(tmpConfigFile, strTest + #13#10,  True);
    end;

    DeleteFile(xmlFile); //delete the old exe.config
    RenameFile(tmpConfigFile,xmlFile);
  end;
end;
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 5

我知道它现在有点老了,但这是另一种方法;使用 MSXML

procedure UpdateConfig();
var
  XMLDoc, NewNode, RootNode, Nodes, Node: Variant;
  ConfigFilename, Key: String;
  i: integer;

begin
  ConfigFilename := ExpandConstant('{app}') + '\your-app-name.exe.config';

  try
      XMLDoc := CreateOleObject('MSXML2.DOMDocument');
  except
    RaiseException('MSXML is required to complete the post-installation process.'#13#13'(Error ''' + GetExceptionMessage + ''' occurred)');
  end;  

  XMLDoc.async := False;
  XMLDoc.resolveExternals := False;
  XMLDoc.load(ConfigFilename);
  if XMLDoc.parseError.errorCode <> 0 then
    RaiseException('Error on line ' + IntToStr(XMLDoc.parseError.line) + ', position ' + IntToStr(XMLDoc.parseError.linepos) + ': ' + XMLDoc.parseError.reason);

  RootNode := XMLDoc.documentElement;
  Nodes := RootNode.selectNodes('//configuration/appSettings/add');
  for i := 0 to Nodes.length - 1 do
  begin
    Node := Nodes.Item[i];
    if Node.NodeType = 1 then
    begin
      key := Node.getAttribute('key');
      Case key of
        'MyValue1' : Node.setAttribute('value', ConfigPage.Values[0]);
        'MyValue2' : Node.setAttribute('value', ConfigPage.Values[1]);
        'MyValue3' : Node.setAttribute('value', ConfigPage.Values[2]);
      end;
    end;
  end;

  XMLDoc.Save(ConfigFilename); 

end;
Run Code Online (Sandbox Code Playgroud)

干杯,马特