【发布时间】:2011-03-25 11:19:01
【问题描述】:
我正在编写一个 Web 应用程序部署验证工具,我需要检查 Web 应用程序和虚拟文件夹的物理文件夹结构。
如何在 C# 中做到这一点?或者更一般地说,通过 C# 代码与 IIS 交互?
【问题讨论】:
我正在编写一个 Web 应用程序部署验证工具,我需要检查 Web 应用程序和虚拟文件夹的物理文件夹结构。
如何在 C# 中做到这一点?或者更一般地说,通过 C# 代码与 IIS 交互?
【问题讨论】:
我强烈建议将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);
给予:
F:\Dev\Branches\Dev1
【讨论】:
【讨论】:
System.DirectoryServices 在 IIS7 上使用 Microsoft.Web.Administration。
看看HttpRequest 对象。您可以使用 HttpContext.Current.Request.Url 获取当前 URL。
如果您想检查虚拟 IIS 文件夹是否存在,您可以尝试对您期望存在的相对 URL 执行 Server.MapPath 调用(同样您会在 HttpContext 对象中找到它)并且如果映射是如果不成功,您可以假设您的相对路径指定的特定子文件夹不存在。
【讨论】: