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

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

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

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

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

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

任何指导深表感谢

问候

增值税

最佳答案

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

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

如果找到匹配项,则用所需标签替换该特定行,该标签的value从我的自定义页面中获取。
   strTest := '  <add key="Name" value="' + strName + '"/> ';

这将被写入临时文件。然后,我删除原始的exe.config文件,并将temp配置文件重命名为exe.config文件(从而反射(reflect)了我需要的更改)。以下是该过程的完整代码段,请不要忘记从[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;

10-07 19:09
查看更多