【问题标题】:Change a web.config programmatically with C# (.NET)使用 C# (.NET) 以编程方式更改 web.config
【发布时间】:2011-01-16 15:33:45
【问题描述】:

如何使用 C# 以编程方式修改/操作web.config?我可以使用配置对象吗?如果可以,如何将web.config 加载到配置对象中?我想要一个更改连接字符串的完整示例。修改后web.config应该写回硬盘。

【问题讨论】:

标签: c# web-config


【解决方案1】:

这是一些代码:

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();

this article 中查看更多示例,您可能需要查看impersonation

【讨论】:

  • web.config 文件中连接字符串的名称是什么?
  • 文章链接已损坏
  • @AlexLE:当 web.config 放在不同的位置时,我们如何指定文件位置。可能是共享路径上的远程服务器?
  • @SharpCoder,路径是虚拟路径,所以您需要先将 web.config 从远程服务器复制到本地应用程序(在临时文件夹中,不要覆盖您自己的 web.config),编辑它,保存然后复制回远程服务器。或者更好、更复杂的解决方案是:创建一个执行工作的 API(更改 de web.config),将其部署到远程服务器,然后从本地服务器使用它。
  • @Skami 您必须将 connectionStrings 名称设置为“MyConnectionString”,这意味着 section.ConnectionStrings["MyConnectionString"] 值应与名称匹配。
【解决方案2】:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
//section.SectionInformation.UnprotectSection();
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();

【讨论】:

  • 如何从section变量中获取用户ID?
【解决方案3】:

由于 web.config 文件是 xml 文件,您可以使用 xmldocument 类打开 web.config。从要更新的 xml 文件中获取节点,然后保存 xml 文件。

这里是详细说明如何以编程方式更新 web.config 文件的 URL。

http://patelshailesh.com/index.php/update-web-config-programmatically

注意:如果您对 web.config 进行任何更改,ASP.NET 会检测到更改并重新加载您的应用程序(回收应用程序池),其效果是保存在 Session、Application 和 Cache 中的数据将丢失(假设会话状态是 InProc 并且不使用状态服务器或数据库)。

【讨论】:

  • 我知道这在技术上是可行的,我认为微软批准/批准的方法可能会更持久,更不容易破坏 web.config。我们都“手动”更新了这些文件,但我认为生产 Web 服务器存在风险。只是我的 2c。
【解决方案4】:

这是我用来更新 AppSettings 的一种方法,适用于 Web 和桌面应用程序。如果您需要编辑连接字符串,您可以从System.Configuration.ConnectionStringSettings config = configFile.ConnectionStrings.ConnectionStrings["YourConnectionStringName"]; 获取该值,然后使用config.ConnectionString = "your connection string"; 设置一个新值。请注意,如果您在 Web.ConfigconnectionStrings 部分中有任何 cmets,这些将被删除。

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

【讨论】:

  • 这段代码很好,我赞成它,但如果 appSettings 在另一个文件中定义,它将不起作用,例如:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
相关资源
最近更新 更多