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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: