Andrew J. Brehm Andrew J. Brehm - 1 year ago 117 Question

Editing Web.config programatically

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

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

Answer Source

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");
   if (objAppsettings != null)
      objAppsettings.Settings["test"].Value = "newvalueFromCode";

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download