5

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

<configuration>
  <appSettings>
    <add key ="Name" value="Brad Pitt"/> 
  </appSettings>
</configuration>

例如:它应通过查询键属性“名称”来读取值。

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

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

<add key ="Name" value="Brad Pitt"/> 

问题是我如何使用 Pascal 脚本或标准脚本来实现这一点。

任何指导都深表感谢

问候

增值税

4

3 回答 3

7

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

   if (Pos('key="Name"', strTest) <> 0 ) 

如果它找到匹配项,那么我用我想要的标签替换该特定行,其中value是从我的自定义页面获取的。

   strTest := '  <add key="Name" value="' + strName + '"/> ';

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

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

代码片段

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;
于 2012-03-07T16:29:34.430 回答
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;

干杯,马特

于 2013-02-11T06:58:08.643 回答
1

只是贡献,这里是对上述过程的更新,现在接收参数,用于任何属性:

procedure UpdateConfigKeyValue(ConfigFilename,NodeName,KeyName,Value:String);
var
  XMLDoc, NewNode, RootNode, Nodes, Node: Variant;
  Key: String;
  i: integer;
begin
  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(NodeName);
  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
        KeyName : Node.setAttribute('value', Value);
      end;
    end;
  end;

  XMLDoc.Save(ConfigFilename); 

end;

使用示例:

UpdateConfigKeyValue(ConfigPath,'//configuration/appSettings/add','hibernate.connection.data_source',SQLServer); 
于 2018-02-07T14:18:57.857 回答