简体   繁体   English

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

[英]How can I manipulate appsettings in a web.config on a remote machine using Microsoft.Web.Administration?

I'm using the Microsoft.Web.Administration library to configure a web.config on a remote machine. 我正在使用Microsoft.Web.Administration库在远程计算机上配置web.config。 I've been able to read sections and add new items for the most part using the GetWebConfiguration method on the Application object, but I've run into a snag with appSettings. 我已经能够使用Application对象上的GetWebConfiguration方法来阅读各节并在GetWebConfiguration添加新项,但是我遇到了与appSettings相关的问题。 I would like to be able to read and write to the appsettings section but I can't seem to find a way to do this and have not found a good example on-line. 我希望能够读写appsettings部分,但是我似乎找不到找到这种方法的方法,也没有找到一个很好的在线示例。

Is this supported using Microsoft.Web.Administration? 使用Microsoft.Web.Administration支持吗? If not, is there another elegant solution? 如果没有,是否还有另一个优雅的解决方案? The solution has to work remotely. 该解决方案必须能够远程工作。

Yes it is, you should be able to do as below, note that I generated the code using the IIS Configuration Editor, see: http://blogs.iis.net/bills/archive/2008/06/01/how-do-i-script-automate-iis7-configuration.aspx 是的,您应该能够执行以下操作,请注意,我是使用IIS配置编辑器生成的代码,请参阅: http : //blogs.iis.net/bills/archive/2008/06/01/how-do -i-脚本自动化- 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();
}

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

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