Edit Web.config at run-time

Posted in ASP.NET, C# by Sumit Thomas on February 21, 2006

[tweetmeme style=”compact”]I had an argument with one of my friends recently on his statement that Web.config cannot be edited at run-time. My counter statement was if it is a XML file and if you have write permission on it then you can do it. Here is the code to do the same…

public class ConfigManager
 //The file path of Web.config
 static string configFilePath =
 /// Returns the Web.config as XmlDocument
 private static XmlDocument GetWebConfig()
 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load( configFilePath );
 return xmlDoc;

 /// Checks for the appSettings node in Web.config
 /// Updates the value for the given key if the key
 /// already exists or creates a new node for the
 /// given key and value combination if it is not present
 public static void CreateAppSetting( string key, string val )
 XmlDocument xmlDoc = GetWebConfig();
 XmlNode xmlNode = xmlDoc.SelectSingleNode("//appSettings");
 if( xmlNode == null )
  throw new Exception("appSettings node not found!");

 string nodeFormat = string.Format("//add[@key='{0}']", key) );

 XmlElement xmlElement =
( (XmlElement) xmlNode.SelectSingleNode( nodeFormat );
  if( xmlElement != null )
   xmlElement.SetAttribute( "value", val );
   xmlElement = xmlDoc.CreateElement( "add" );
   xmlElement.SetAttribute( "key", key );
   xmlElement.SetAttribute( "value", val );
   xmlNode.AppendChild( xmlElement );
  SaveWebConfig( xmlDoc );
 catch( Exception ex )
  throw new Exception( ex.Message );


 /// Saves the changes to the Web.config file
 private static void SaveWebConfig( XmlDocument xmlDoc )
  XmlTextWriter writer =
 new XmlTextWriter( configFilePath, null );
  writer.Formatting = Formatting.Indented;
  xmlDoc.WriteTo( writer );
  catch( Exception ex )
  throw new Exception( ex.Message );
}//end of class

The application should have required permissions on Web.config to edit it, otherwise the code will throw an access denied error.

Technorati: , ,
Tagged with: ,

12 Responses

Subscribe to comments with RSS.

  1. James said, on April 7, 2006 at 5:43 pm

    I have tried to use your method and I couldn’t get pass the “Access Denied” error.

    I have given Everyone full rights on web.config file and still no luck.

    Am I missing something?

  2. Sabir said, on November 17, 2006 at 12:28 am

    I am getting an error in Web.config file when i am hosting an asp.net web application to ftp domain.can u please help me it’s verry urgent.

  3. Savage said, on November 17, 2006 at 7:46 am

    It would be useful if you could post the error that you received

  4. Matthew Cosier said, on January 14, 2007 at 4:49 am

    Configuration config;
    config = WebConfigurationManager.OpenWebConfiguration(“~”);
    CompilationSection compilation = config.GetSection(“system.web/compilation”) as CompilationSection;

    if (compilation != null)

    compilation.Debug = !compilation.Debug;

  5. soby said, on January 29, 2007 at 6:01 am

    This is really interesting

  6. Antonino Ferrando said, on March 21, 2007 at 1:48 pm

    Muy bueno!! Funciona a la perfección, aunque lo tuve que pasar a VB.NET.
    Un abrazo y gracias!


  7. Antonino Ferrando said, on March 21, 2007 at 1:50 pm

    sorry, now in english.

    Really good! It works perfecly, although I had to translate it to VB.NET
    A hugh and thanks a lot!

    AUS Antonino Ferrando
    Universitarian Systems Analyst
    Santa Fe – Argentina

  8. Sheron said, on September 14, 2007 at 6:58 am

    Thanks for the post. Proved very useful when trying to edit the Web.config using a custom install action in a web setup project.

    However I ran into a slight prob with the XPath expression you’d used. It would work fine with a ASP.NET 1.1 Web.config. But the Web.config used in a 2.0 solution has an XML namespace defined in its root node. Like this <configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0″>

    When I used the line of code,

    XmlNode xmlNode = xmlDoc.SelectSingleNode(”//appSettings”);

    the xmlNode object was always null.

    The problem seems to be the one outlined in Wayne Allen’s blog http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx .

    I tried using the solution he’s offered and now everything works just fine.


  9. 2leggedspider said, on September 14, 2007 at 8:33 am

    Thanks Sheron! That was an useful link.

    There has been few changes to web.config in ASP.NET 2.0. Fortunately there are new classes available to manipulate the web.config programatically.

    The following code does the same trick in 2.0

    private void ModifyWebConfig(string key, string value)
    Configuration config = WebConfigurationManager.OpenWebConfiguration(“~”);
    AppSettingsSection appSetting = (AppSettingsSection)config.GetSection(“appSettings”);
    if (appSetting != null)
    appSetting.Settings[key].Value = value;

  10. Martin said, on October 19, 2007 at 8:17 am

    Nice one!

  11. Addon said, on February 15, 2012 at 6:14 pm


  12. www.dbvehicleelectrics.com said, on August 10, 2014 at 2:12 pm

    Fantastic goods from you, man. I have be mindful your stuff prior to and you are
    simply extremely excellent. I really like what you have received right here,
    really like what you’re stating and the way in which wherein you
    are saying it. You’re making it enjoyable and you still take care of to stay it wise.
    I can not wait to learn far more from you. That is actually a wonderful site.

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: