How to edit app.config file?

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.

Step 2: Add reference to System.Configuration

Add reference to System.Configuration namespace

Step 3: Add code to edit config

System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            configuration.AppSettings.Settings.Remove(“font”);
            configuration.AppSettings.Settings.Add(“font”, “Calibri”);

            configuration.Save(ConfigurationSaveMode.Modified);

Summary

For editing an existing appSettings key – we have deleted and added the key.

Leave a Reply

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