Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing Web.config programmatically

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

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

like image 656
Andrew J. Brehm Avatar asked Nov 06 '08 21:11

Andrew J. Brehm


1 Answers

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.

like image 62
DOK Avatar answered Oct 27 '22 15:10

DOK