locked
How do I (update/insert/remove) the config file during runtime? RRS feed

  • Question

  • How do I (update/insert/remove) the config file during runtime?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 7, 2009 9:29 AM

Answers

  • Insert new values into the config file during runtime.

    Generally, we use ConfigurationManager to update the content of the application configuration file.

    Here is the original App.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="Setting1" value="1" />
        <add key="Setting2" value="2" />
      </appSettings>
    </configuration>
    
    

    The code below is used to add a new appSetting value into the configuration file.

       public Form1()
            {
                InitializeComponent();
            }
    
            private void ShowConfig()
            {
                // For read access you do not need to call the OpenExeConfiguraton
                foreach (string key in ConfigurationManager.AppSettings)
                {
                    string value = ConfigurationManager.AppSettings[key];
                    MessageBox.Show(String.Format("Key: {0}, Value: {1}", key, value));
                }
            }
    
            private void UpdateConfig()
            {
                // Open App.Config of executable
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
                // Add an Application Setting.
                config.AppSettings.Settings.Add("Setting3", "3");
                // Save the configuration file.
                config.Save(ConfigurationSaveMode.Modified, true);
                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ShowConfig();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                UpdateConfig();
            }
    
    

    Modify existing values in the config file during runtime.

    Because the Configuration.AppSettings property is read-only, in order to modify the current application settings value, we must use the XmlDocument class to directly update the application configuration file as an XML document.

    Here is the original App.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="Setting1" value="1" />
        <add key="Setting2" value="2" />
      </appSettings>
    </configuration>
     

    Here is the code sample to modify the application settings value:

    XmlDocument xmlDoc = new XmlDocument();
    
    xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    
    foreach (XmlElement element in xmlDoc.DocumentElement)
    {
        if (element.Name.Equals("appSettings"))
        {
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node.Attributes[0].Value.Equals("Setting1"))
                {
                    node.Attributes[1].Value = "New Value";
                }
            }
        }
    }
    
    xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    
    ConfigurationManager.RefreshSection("appSettings");
    
    

    Related thread:
    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/0a1fc870-9ad1-4f66-9fc1-8d28cda46a7e/

    For more FAQ about Visual C# General, please see Visual C# General FAQ


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Tuesday, April 7, 2009 9:30 AM