简体   繁体   English

使用 C# (.NET) 以编程方式更改 web.config

[英]Change a web.config programmatically with C# (.NET)

How can I modify / manipulate the web.config programmatically with C# ?如何使用 C# 以编程方式修改/操作web.config Can I use a configuration object, and, if yes, how can I load the web.config into a configuration object ?我可以使用配置对象吗,如果可以,我如何将web.config加载到配置对象中? 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写回硬盘。

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 .查看本文中的更多示例,您可能需要看看模拟

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
//section.SectionInformation.UnprotectSection();
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();

Since web.config file is xml file you can open web.config using xmldocument class.由于 web.config 文件是 xml 文件,您可以使用 xmldocument 类打开 web.config。 Get the node from that xml file that you want to update and then save xml file.从要更新的 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 而不是使用状态服务器或数据库)。

This is a method that I use to update AppSettings, works for both web and desktop applications.这是我用来更新 AppSettings 的一种方法,适用于 Web 和桌面应用程序。 If you need to edit connectionStrings you can get that value from System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"];如果您需要编辑 connectionStrings,您可以从System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"];获取该值System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"]; and then set a new value with config.ConnectionString = "your connection string";然后使用config.ConnectionString = "your connection string";设置一个新值config.ConnectionString = "your connection string"; . . Note that if you have any comments in the connectionStrings section in Web.Config these will be removed.请注意,如果您在Web.ConfigconnectionStrings部分中有任何注释,这些注释将被删除。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM