如何得到指定虚拟目录名称的实际路径

来源:互联网 发布:澳洲硕士 知乎 编辑:程序博客网 时间:2024/05/24 06:49

由于刚好需要这么一个功能,因此在网上找了很久没有找到一个我想要的。。把所有的查询结果全部整理了下,然后自己修改了下终于得到我想要的结果,特此留个记号。

 

 

//返回的是指定虚拟目录的实际路径;

 public  string GetWebVirtualDirectoryPath(string iisName)
        {
            string iisPath = null;
            try
            {
                DirectoryEntry _entry = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");//此处现在只能读取到默认的80端口
                DirectoryEntries _entries = _entry.Children;
                foreach (DirectoryEntry _cen in _entries)
                {
                    if (_cen.Name.ToLower() == iisName.ToLower())
                    {
                        DirectoryEntry root = new DirectoryEntry(_cen.Path);
                        iisPath = root.Properties["Path"].Value.ToString();
                    }
                   
                    //   string aa = _cen.Properties["Path"].Value.ToString();IIS://localhost/W3SVC/1/Root/bbb

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return iisPath;
        }