从ServerManager对象访问Web.Config路径

时间:2013-12-13 13:06:11

标签: c# asp.net iis

我需要更新一个远程Web.config文件,我目前可以通过ServerManager程序集中的Microsoft.Web.Administration对象访问该应用程序,如下所示:

ServerManager serverManager = ServerManager.OpenRemote("::1");
Configuration configuration = serverManager.GetWebConfiguration("Default Web Site", "/Application.Name");

我是否能够从此对象检索Web.config的路径,以便将其加载到System.Xml.Linq.XDocument并进行修改?

我最初的想法是使用ConfigurationElementCollection appSettings = configuration.GetSection("appSettings").GetCollection();,但我无法使用此方法访问我需要修改的配置数据,因此需要使用XDocument

1 个答案:

答案 0 :(得分:3)

我终于找到了它。看起来访问它的唯一方法是:

ServerManager manager = ServerManager.OpenRemote("::1");
string physicalPath = manager.Sites[siteName].Applications[virtualDirPath].VirtualDirectories[0].PhysicalPath

string webConfigLocation = Path.Combine(physicalPath, "web.config");