Change a web.config programmatically with C# (.NET)
Here it is some code:
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
See more examples in this article, you may need to take a look to impersonation.
update web.config through c# code
Will you please try this ?
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();
}
}
Or Please refer link Editing Web.config programatically
Editing Web.config programmatically
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.
modify web.config programmatically c# vs2010
I think you are looking for something like this:
http://foreachbiscuit.wordpress.com/2007/10/11/custom-webconfig-settings/
Related Topics
Creating a Generic<T> Type Instance with a Variable Containing the Type
Detect If Running as Administrator with or Without Elevated Privileges
Calling Async Method Synchronously
Accessing a Variable from Another Script C#
Any Decent C# Profilers Out There
How to Detect the Currently Pressed Key
How to Change Datatype of a Datacolumn in a Datatable
Code for Decoding/Encoding a Modified Base64 Url (In ASP.NET Framework)
How to Register a Global Hot Key to Say Ctrl+Shift+(Letter) Using Wpf and .Net 3.5
What Does Initializecomponent() Do, and How Does It Work in Wpf
How to Run Commands by Sudo and Enter Password by Ssh .Net C#