根据应用程序池名称获得第一个站点的物理路径
依赖DLL:Microsoft.Web.Administration.dll
/// <summary> /// 根据应用程序池名称获得第一个站点的物理路径 /// </summary> /// <param name="appPoolName">应用程序池名称</param> /// <returns></returns> private static string GetPhysicalPath(string appPoolName) { var physicalPath = string.Empty; using (var sm = new ServerManager()) { foreach (var site in sm.Sites) { var app = site.Applications.Where(i => i.ApplicationPoolName.Equals(appPoolName)).Single(); if (app == null) { continue; } var virtualRoot = app.VirtualDirectories.Where(v => v.Path == "/").Single(); if (virtualRoot != null) { physicalPath = virtualRoot.PhysicalPath; break; } } } return physicalPath; }
老外的一篇不错的文章:https://johnlnelson.com/2014/06/15/the-microsoft-web-administration-namespace/