views:

1298

answers:

8

What is a good way to edit a Web.config file programatically?

I looked into System.Xml but couldn't find any obvious answers.

Incidentally, ctrl-k seems to insert a code sample tag in the input box here. This is a bit annoying as ctrl-k is typically (in Emacs and Cocoa applications) used for killing a line of text.

A: 

What is the purpose of editing your config file programmatically? More details would be helpful.

Anders
Sorry, I thought it was obvious. It's for deployment/installation of the Web application.
Andrew J. Brehm
+1  A: 

Depending on what you are doing, the method is really a bit different in each situation. However the most robust method is to load it as an XmlDocument and modify it as needed via that method, but you MUST be careful to only modify it in the needed manner.

Mitchel Sellers
+2  A: 

You can use the WebConfigurationManager to read specific configuration sections. This will return a ConfigurationSection object. You can use this to read/modify the ConfigurationElements in the section. Once you have updated them, you can Save the ConfigurationSection and it will update the file with your changes.

I use this to automatically encrypt the appSettings and connectionStrings on Application_Start if they aren't already encrypted. I haven't actually changed any settings this way, but it seems like you ought to be able to do so.

Saving the updated configuration file may cause the app to recycle depending on how it is built.

tvanfosson
A: 

Yes I agree with Josh. I have tried this before and I've had two negative effects:

  1. Slow loading if the current page after postback because ASP.NET is loading the web.config and all related resources
  2. If you change the web.config early enough in the load cycle (e.g. global.asax events) the site may never load or fail in unpredictable ways
mjmarsh
+1  A: 

In theory; you could just generate a web config file programmatically and with some templating to make it easy.

However, if you're trying to edit your web.config from within the site; it's highly recommended you don't. At the very least; you'd trigger an app reset every time you updated it; which would be especially bad if you're using in-process sessions.

As Anders asked, what is it you're trying to do?

Frank Rosario
+2  A: 

This fellow shows sample code if you still want to do it after all the caveats:

protected void EditConfigButton(object sender, EventArgs e)
{
   Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
   AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
   //Edit
   if (objAppsettings != null)
   {
      objAppsettings.Settings["test"].Value = "newvalueFromCode";
      objConfig.Save();
   }
}

One valid reason for editing a web.config is to encrypt it, which is what that article is about.

DOK
A: 

Agree with others, editing the webconfig is achievable, but has knock on effects are just to dangerous / risk involved

If its a value that is application specific, then it should be in an application specific config file

spacemonkeys
A: 

Lot of time you want to modify application specific settings after deployment like say when something is wrong e.g. switching the database connection in case current DB goes down. Moreover sometimes you want to create your own XML based configuration file which you want o modify programatically.

Try XML Webpad - http://xmlwebpad.codeplex.com/

Its a framework to view an edit XML files. Once you integrate it with your web app, editing web.config ill be as simple as viewing the web.config page, making the required changes and hitting the save button (all from within your application).

netbuild