How to change the key value present in web config dynamically

Learn how to change the key value present in web config dynamically, in this article you will know Dynamically change key value in web.config.

Dynamically change key value in web.config

Suppose there is a key present in web config and which have the value 10.


<add key="Unit" value="10" />

You can modify the value programmatically using this code

Code Behind

Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
webConfigApp.AppSettings.Settings["Unit"].Value = "15";

Namespace Required:

using System.Configuration;
using System.Web.Configuration;

Article by Abhay Kumar Raut
DNS MVM and .Net Guru Team Lead at CirrusCloud Systems Bhubaneswar,Odisha Thanks and Regards, Abhay Kumar Raut

Follow Abhay Kumar Raut or read 39 articles authored by Abhay Kumar Raut


Author: Abhay Kumar Raut30 Oct 2011 Member Level: Gold   Points : 0

Why Should we use that framework while we have facility to change it through writing code? Can you explain?


Guest Author: Udayan07 Apr 2012

I believe the idea of using a framework is to let you write as less code as possible (i.e. not re-invent the wheel by starting from scratch) but still give the flexibility of extending/changing it with your own code per your requirement.

Guest Author: abhay11 Apr 2012

I believe this code is not huge..It's one line of code and programmer don't change value frequently in webconfig.I believe it's not good idea to use a framework instead of writing one line of code.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: