The application configuration file provides flexibility in reading application settings. But once in a while it is needed to change the settings programmatically. We can see how it is made possible.
Step 1: Create new Windows Forms application
Create a new windows forms application and add a config file into it.
Replace the content of app.config with following:
Add two buttons to the form named read and write.
On the read button click add the following code:
string value = ConfigurationManager.AppSettings["font"];
MessageBox.Show(value);Step 2: Add reference to System.Configuration
The System.Configuration namespace is required for getting the required functionality.
Step 3: Add code to edit config
Now we are ready to write the appSettings editing code. Click on the write button from the form and
add the following code to it.
System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
For editing an existing appSettings key - we have deleted and added the key.
Run the Application
Execute the application and you can test the editing of configuration using the read/write buttons.