Archive

Posts Tagged ‘installer’

C# Update app.config in msi custom action

May 12th, 2011 3 comments

I have been doing quite a lot of googling about updating the app.config file during an installation using an msi custom action. I found this blog by Rob Aquila, however this did not really work for me.

I then came across this question on StackOverflow that almost solved the problem. The key to the solution was to open the app.config and save directly to this file.

The issue I found with the later solution is that for some reason I could not get the Context.Parameters = “targetdir”. When the install was running this was always returning null.

So my solution that worked for me is as follows:

public override void Install(System.Collections.IDictionary savedState)
{
    base.Install(savedState);

    var targetAssembly = Context.Parameters["assemblypath"];

    var path = string.Format("{0}.config", targetAssembly);

    var xmlDoc = new System.Xml.XmlDocument();

    xmlDoc.Load(path);

    var node = xmlDoc.SelectSingleNode("/configuration/userSettings/Application.Properties.Settings/setting[@name='SettingName']/value");
    node.InnerText = "PLACE NEW PARAMATER HERE";

    xDoc.Save(path);
}

Thank you to Khalil Dahab for his response on StackOverflow

%d bloggers like this: