代码之家  ›  专栏  ›  技术社区  ›  BenR

如何使用Microsoft.web.Administration在远程计算机上操纵web.config中的appsettings?

  •  0
  • BenR  · 技术社区  · 14 年前

    GetWebConfiguration 上的方法 Application 对象,但我遇到了appSettings的问题。我想能够读和写的appsettings部分,但我似乎找不到一个方法来做到这一点,并没有找到一个很好的例子在线。

    使用Microsoft.Web.Administration是否支持此操作?如果没有,还有其他优雅的解决方案吗?解决方案必须远程工作。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Carlos Aguilar Mares    14 年前

    http://blogs.iis.net/bills/archive/2008/06/01/how-do-i-script-automate-iis7-configuration.aspx

    using(ServerManager mgr = ServerManager.OpenRemote("Some-Server")) {
    
      Configuration config = mgr.GetWebConfiguration("site-name", "/test-application");
    
      ConfigurationSection appSettingsSection = config.GetSection("appSettings");
    
      ConfigurationElementCollection appSettingsCollection = appSettingsSection.GetCollection();
    
      ConfigurationElement addElement = appSettingsCollection.CreateElement("add");
      addElement["key"] = @"NewSetting1";
      addElement["value"] = @"SomeValue";
      appSettingsCollection.Add(addElement);
    
      serverManager.CommitChanges();
    }