[ACCEPTED]-Editing Web.config programmatically-web-config

Accepted answer
Score: 14

This fellow shows sample code if you still want to 3 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");
   if (objAppsettings != null)
      objAppsettings.Settings["test"].Value = "newvalueFromCode";

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

Score: 4

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

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

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

Score: 2

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

Score: 2

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

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

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

Score: 1

Yes I agree with Josh. I have tried this 1 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
Score: 1

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

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

Score: 1

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

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

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

More Related questions