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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
This is a method that I use to update AppSettings, works for both web and desktop applications. If you need to edit connectionStrings you can get that value from
System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"];
and then set a new value withconfig.ConnectionString = "your connection string";
. Note that if you have any comments in theconnectionStrings
section inWeb.Config
these will be removed.Here it is some code:
See more examples in this article, you may need to take a look to impersonation.
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.
here is URL that explains in more detail how you can update web.config file programmatically.
http://patelshailesh.com/index.php/update-web-config-programmatically
Note: if you make any changes to web.config, ASP.NET detects that changes and it will reload your application(recycle application pool) and effect of that is data kept in Session, Application, and Cache will be lost (assuming session state is InProc and not using a state server or database).