How can I modify / manipulate the web.config
programmatically with C# ? Can I use a configuration object, and, if yes, how can I load the web.config
into a configuration object ? I would like to have a full example changing the connection string. After the modification the web.config
should be written back to the harddisk.
Since web. config file is xml file you can open web. config using xmldocument class. Get the node from that xml file that you want to update and then save xml file.
The machine. config file file is at the highest level in the configuration hierarchy while Web. config file is to override the settings from the machine. config file.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With