如何从Innosetup脚本修改exe.config [英] how to modify the exe.config from Innosetup script
问题描述
我已经开始自己学习Innosetup脚本.为此,我创建了一个简单的C#控制台应用程序,该应用程序从配置文件中读取元素并将其输出到控制台上.
I've started to learn Innosetup scripting by myself. For this i have created a simple C# console application, which reads an element from a configuration file and outputs onto the console.
<configuration>
<appSettings>
<add key ="Name" value="Brad Pitt"/>
</appSettings>
</configuration>
例如:它将通过查询键属性名称"来读取值.
For ex: It shall read the value by querying the key attribute "Name".
我希望从Innosetup安装脚本中写入.config中的值.
I want the value in the .config to be written from the Innosetup setup script.
即在安装过程中,我将收集名称(在这种情况下为"Brad Pitt")并将其写入配置文件的值
i.e During the installation process i shall gather the name (i.e "Brad Pitt" in this case) and write it to the value of the config file
<add key ="Name" value="Brad Pitt"/>
问题是如何使用Pascal脚本或标准脚本来实现这一目标.
Question is how do i achieve this, using a Pascal script or a standard script.
任何指导深表感谢
致谢
VATSA
推荐答案
为此,我创建了一个简单的过程,该过程将xml文件名作为输入.该过程将解析每一行并将内容写入临时文件.该代码检查每行以查找字符串'key ="Name"':
To achieve this I created a simple procedure, which takes the xml file name as input. The procedure shall parse each line and write the contents to a temp file. The code checks each line looking for the string 'key="Name"':
if (Pos('key="Name"', strTest) <> 0 )
如果找到匹配的内容,则用所需的标记替换该特定行,其中的value
从我的自定义页面中获取.
If it finds a match then I replace that particular line by my desired tag, of which the value
is gotten from my custom page.
strTest := ' <add key="Name" value="' + strName + '"/> ';
这将被写入临时文件.然后,我删除原始的exe.config文件,并将temp配置文件重命名为exe.config文件(从而反映了我需要的更改).以下是该过程的完整代码段,请不要忘记从[文件]调用该过程,即
This gets written into a temp file. I then delete the original exe.config file and rename the temp config file to the exe.config file (thus reflecting the changes I need). Below is the entire code snippet for the procedure, and don't forget to call the procedure from [Files] i.e.
[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;
这篇关于如何从Innosetup脚本修改exe.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!