Musielak Marek

Posted on Tuesday, March 03, 2009 by Marek Musielak

The easiest way of editing web.config dynamically

Some time ago I needed to update web.config programatically from my web site in order to add my own key to <appSettings /> tag. I spent a lot of time looking for a solution on the Internet. I wasted a lot of time trying to reuse existing code from the Web but nothing worked for me. I most cases they thrown an exception, some didn't even compile. Finally I found one on my own.


First lets read existing configuration and value of the settings element:
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["MyValue"];

if (null != setting)
{
// lets do something with the value - displaying it in a textbox is an ides
textboxValue.Text = setting.Value;
}
}
}

We can use web.config file from some other directory if we use its name instead of '~' in WebConfigurationManager.OpenWebConfiguration("~") method.

We've already read the value and displayed it in textbox so we are ready to save updated value (or add new settings element if it hasn't been saved yet).
protected void btnSave_Click(object sender, System.EventArgs e)
{
try
{
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["MyValue"];

if (null != setting)
{
config.AppSettings.Settings["MyValue"].Value = textboxValue.Text;
}
else
{
config.AppSettings.Settings.Add("MyValue", textboxValue.Text);
}

config.Save();
literalMessage.Text = "New value saved. Application will be restarted automatically.";
}
catch (System.Exception exc)
{
literalMessage.Text = (exc.Message + "<br />" + exc.StackTrace).Replace("\n", "<br />");
}
}


And that's all. The solution is short so the post is short ;) Any comments? Do not hesitate to write them below.

Shout it
Read More »

Related Posts:


4 Responses to The easiest way of editing web.config dynamically

  1. Marcin Rybacki
    March 14, 2009 at 8:58 AM
    There's one thing to be cautious here.

    If this modification is made inside ASP.NET Framework, then most likey this is handled with ASPNET account. This account by default has no write permissions for *.config files (please correct me if I'm mistaken). One of the options if to grant ASPNET that kind of access (which is not too secure). And onother option is to write you own WinForms or Console Application which is ran by the current user and which lacks this kind of restriction.
  2. Marek Musielak
    March 14, 2009 at 10:37 AM
    You're absolutely right. You need to have rights of modification web.config by the ASPNET account. However, I would not agree that granting such an access is not too secure. One should just ensure that there is no way of editing configuration if user is not authorized for those kinds of operations.

    And one more thing that is frequently forgotten: if one changes web.config, web application is restarted. It has to be taken into account.
  3. Anonymous
    June 26, 2013 at 10:55 AM
    excuse my ignorance but could this be used to update the web.config file with imput from the user?

    So for example if I type an IP address into a textbox, could it then automatically be added to web.config so that the IP will from then on be a permanent part of the web.config?

    Sorry, very new to this
  4. Marek Musielak
    June 26, 2013 at 1:30 PM
    Of course. Just remember that this is risky, as you need to remember about access rights and possibilities of introducing errors which can crash you application.

Post a Comment