.Net ConfigurationManager saving AppSettings

Well, it has been a while since I posted anything code related.  I have been working part time for a new start up company SimpleWorks, developing software for the pc (instead of a web application), and in this project I seem to always be learning something new.  I wanted to use the App.config to store some application specific settings that can be configured by the user.  Sounded like a simple task.  Open the App.config and begin making my entries in the XML data.  Successfully used ConfigurationSettings.AppSettings.Get() to retrieve the settings from the App.config. 


I could also use ConfigurationSettings.AppSettings.Set() to change or set the values, but these changes never appeared in the App.config file.  And there is no AppSettings.Save() method to save the setting changes to the file.  So I searched and searched, and tried a few suggestions, but it was not till I read this post… all the way to the bottom… that I finally found a solution, thanks to Charlie.


It seems that if you use ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath) to get a reference to your configuration (app.config), you then have access to a Save() method.  See the code below.


// Load the application’s configuration file.
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

// Set the key = value
config.AppSettings.Settings["<SomeKey>"].Value = <SomeValueToAssignToTheKey>;

// Save the changes back to the original configuration file

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.