Changing App.config at Runtime

Posted by born to hula on Stack Overflow See other posts from Stack Overflow or by born to hula
Published on 2010-01-05T20:20:17Z Indexed on 2010/04/13 12:02 UTC
Read the original article Hit count: 484

I'm writing a test winforms / C# / .NET 3.5 application for the system we're developing and we fell in the need to switch between .config files at runtime, but this is turning out to be a nightmare.

Here's the scene: the Winforms application is aimed at testing a WebApp, divided into 5 subsystems. The test proccess works with messages being sent between the subsystems, and for this proccess to be sucessful each subsystem got to have its own .config file.

For my Test Application I wrote 5 separate configuration files. I wish I was able to switch between these 5 files during runtime, but the problem is: I can programatically edit the application .config file inumerous times, but these changes will only take effect once. I've been searching a long time for a form to address this problem but I still wasn't sucessful.

I know the problem definition may be a bit confusing but I would really appreciate it if someone helped me.

Thanks in advance!

--- UPDATE 01-06-10 ---

There's something I didn't mention before. Originally, our system is a Web Application with WCF calls between each subsystem. For performance testing reasons (we're using ANTS 4), we had to create a local copy of the assemblies and reference them from the test project. It may sound a bit wrong, but we couldn't find a satisfying way to measure performance of a remote application.

--- End Update ---

Here's what I'm doing:

public void UpdateAppSettings(string key, string value)
    {

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        foreach (XmlElement item in xmlDoc.DocumentElement)
        {
            foreach (XmlNode node in item.ChildNodes)
            {

                if (node.Name == key)
                {
                    node.Attributes[0].Value = value;
                    break;
                }
            }
        }
        xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

        System.Configuration.ConfigurationManager.RefreshSection("section/subSection");


    }

© Stack Overflow or respective owner

Related posts about app.config

Related posts about configuration-files