为此,我创建了一个简单的过程,它将 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;