Home > C# Snippets, Development > C# Update app.config in msi custom action

C# Update app.config in msi custom action

May 12th, 2011

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

  1. October 18th, 2012 at 17:48 | #1

    Hello, just wanted to tell you, I liked this article.
    It was practical. Keep on posting!

  2. April 23rd, 2013 at 20:50 | #2

    Marvelous, what a web site it is! This website provides valuable data to us, keep it up.

  3. July 15th, 2014 at 17:02 | #3

    Good write-up. I definitely appreciate this website.
    Keep it up!

Comments are closed.
%d bloggers like this: