如何使用C#代码获取IIS虚拟目录和Web应用程序的物理路径?

smw*_*dia 3 .net windows iis

我正在用C#编写一个Web应用程序部署验证工具.我需要检查Web Applicatons和虚拟文件夹的物理文件夹结构.这可以在C#中完成吗?或者更一般地说,通过C#代码与IIS交互?

我找到了这个链接,并将它读回家.

谢谢.

Swa*_*aff 13

我强烈建议使用Microsoft.Web.Administration dll为IIS提供出色的管理功能.

ServerManager serverManager = new ServerManager();

// get the site (e.g. default)
Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
// get the application that you are interested in
Application myApp = site.Applications["/Dev1"];

// get the physical path of the virtual directory
Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath);
Run Code Online (Sandbox Code Playgroud)

得到:

F:\开发\分行\ DEV1