Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Created January 24, 2015 03:34
Show Gist options
  • Save nsdevaraj/27d0b55ed36f964476f5 to your computer and use it in GitHub Desktop.
Save nsdevaraj/27d0b55ed36f964476f5 to your computer and use it in GitHub Desktop.
inno
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
//alles in string array speichern
LoadStringsFromFile(xmlFile, xmlInhalt);
//durch Array iterieren
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;
shareimprove this answer
answered Mar 7 '12 at 16:29
this-Me
7211236
add a comment
up vote
2
down vote
I know it's a bit old now but here's another approach; use 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment