.NET使用ServerManager获取网站物理路径

最近因为工作需要,用wpf做了一个辅助小工具,如下图

为了获取网站的物理路径,我分析了通过ServerManager获取到的变量,也通过百度搜索了很多,但仍然没有找到方法。

后来使用必应,在国外网站找到了,不知道是不是没有正确的打开方式,所以经历如此曲折,下面我把获取物理路径的方法写下来供大家参考,希望对后人有所帮助。

 1 private string GetWebSitePhysicalPath(Site site)
 2         {
 4             var applicationRoot = site.Applications.Where(a => a.Path == "/").Single();
 5             var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single();
 6             string ret = virtualRoot.PhysicalPath;
 7             string[] strList = ret.Split('%').Where(t => t != "%").ToArray();
 8             ret = "";
 9             for (int i = 0; i < strList.Length; i++)
10             {
11                 string sPath = Environment.GetEnvironmentVariable(strList[i].ToLower());
12                 if (string.IsNullOrEmpty(sPath))
13                 {
14                     ret += strList[i];
15                 }
16                 else
17                 {
18                     ret += sPath;
19                 }
20             }
21             return ret;
22         }

参考资料:

[1]https://stackoverflow.com/questions/5431703

猜你喜欢

转载自www.cnblogs.com/zhuxiaoxiao/p/9495897.html
今日推荐