使用 C# (.NET) 以编程方式更改 web.config
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2260317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change a web.config programmatically with C# (.NET)
提问by Kottan
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.
如何web.config
使用 C#以编程方式修改/操作?我可以使用web.config
配置对象吗,如果可以,我如何将其加载到配置对象中?我想要一个更改连接字符串的完整示例。修改后web.config
应该写回硬盘。
采纳答案by Alex LE
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.
回答by ASergan
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
//section.SectionInformation.UnprotectSection();
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
回答by shailesh
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.
由于 web.config 文件是 xml 文件,您可以使用 xmldocument 类打开 web.config。从要更新的 xml 文件中获取节点,然后保存 xml 文件。
here is URL that explains in more detail how you can update web.config file programmatically.
这里的 URL 更详细地解释了如何以编程方式更新 web.config 文件。
http://patelshailesh.com/index.php/update-web-config-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).
注意:如果您对 web.config 进行任何更改,ASP.NET 会检测到这些更改并将重新加载您的应用程序(回收应用程序池),其影响是保存在会话、应用程序和缓存中的数据将丢失(假设会话状态是 InProc 而不是使用状态服务器或数据库)。
回答by Ogglas
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 with config.ConnectionString = "your connection string";
. Note that if you have any comments in the connectionStrings
section in Web.Config
these will be removed.
这是我用来更新 AppSettings 的一种方法,适用于 Web 和桌面应用程序。如果您需要编辑 connectionStrings,您可以从中获取该值System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"];
,然后使用config.ConnectionString = "your connection string";
. 请注意,如果您在该connectionStrings
部分中有任何评论,Web.Config
这些内容将被删除。
private void UpdateAppSettings(string key, string value)
{
System.Configuration.Configuration configFile = null;
if (System.Web.HttpContext.Current != null)
{
configFile =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
}
else
{
configFile =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
var settings = configFile.AppSettings.Settings;
if (settings[key] == null)
{
settings.Add(key, value);
}
else
{
settings[key].Value = value;
}
configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
}