How to Add/Update/Remove Application Configuration (App.Config) Keys

14/05/2013

How to add new keys to app.config file?

Simple, just use this code.

  Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  conf.AppSettings.Settings.Add("Menol", "value");
  conf.Save(ConfigurationSaveMode.Modified);
  /* Copyright 2013 © I Learnt Today */

When you execute this code, a new key will be added to your configuration file and the given value will be set for it.

Important: This code will not change the configuration file you see in the Visual Studio but the code works. You can check it by copying your exe and app.config file to somewhere and running the exe file.

How to update/modify a key on app.config file?

Use the code below:

 Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 conf.AppSettings.Settings["Menol"].Value = "New Value";
 conf.Save(ConfigurationSaveMode.Modified);
 /* Copyright 2013 © I Learnt Today */

How to remove/delete a key on app.config file?

Use the code below:

 Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 conf.AppSettings.Settings.Remove("Menol");
 conf.Save(ConfigurationSaveMode.Modified);
 /* Copyright 2013 © I Learnt Today */

Menol
ILT

This entry was posted in C#.Net, Microsoft .Net Framework and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

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