最近在做自动安装BS系统时,遇到需要获取虚拟目录对应的物理路径的问题,稍微整理一下,分享给大家!
///
/// 获取虚拟目录对应的物理路径
///
/// 所在站点端口端口号
/// 虚拟目录名称
///
public string GetVirtualDirectory(string _portNumber, string _name)
{
// 获取网站的标识符,默认为1
string identifier = null;
DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
foreach (object property in e.Properties["ServerBindings"])
{
if (property.Equals(":" + _portNumber + ":"))
{
identifier = e.Name;
break;
}
}
if(identifier != null)
{
break;
}
}
}
if(identifier != null)
{
identifier = "1";
}
DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + _name);
string path = (string)de.Properties["Path"].Value;
return path;
}