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.

Example:

<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";
webConfigApp.Save();


Namespace Required:

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


Comments

Author: Udayan03 Jul 2010 Member Level: Bronze   Points : 2

I would recommend XML Webpad - http://xmlwebpad.codeplex.com/

It's a framework to view an edit XML files. Once you integrate it with your web app, editing web.config will be as simple as viewing the web.config page, making the required changes and hitting the save button (all from within your application). And bets part all the editing features comes out-of-box and you don't have to write any code for that.

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?

Thanks,
Abhay

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:
    Email: