列出IIS 6.0中的虚拟目录 - C#

时间:2011-09-16 10:53:49

标签: c# iis-6

我编写了一个Windows应用程序。我的问题是:我一直在IIS 6.0中列出虚拟目录,其代码如下所示。我必须找到所选虚拟目录的pyhsical路径。   此外,DirectoryEntry类具有名为properties的属性。但是,我不能使用它。最后,我得到以下错误。

   The directory cannot report the number of properties

代码:

  try

  {

  string serverName = "localhost";

  string VirDirSchemaName = "IIsWebVirtualDir";

  iisServer = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1");

  DirectoryEntry folderRoot = iisServer.Children.Find("Root",VirDirSchemaName);

  return folderRoot.Children;

  }

  catch (Exception e)

  {

  throw new Exception("Error while retrieving virtual directories.",e);

  }

1 个答案:

答案 0 :(得分:1)

为什么不使用WMI

 using System.DirectoryServices;

    private DirectoryEntry _iisServer = null;
    private DirectoryEntry iisServer
    {
        get
        {
            if (_iisServer == null)
            {
                string path = string.Format("IIS://{0}/W3SVC/1", serverName);
                _iisServer = new DirectoryEntry(path);
            }
            return _iisServer;
        }
    }

    private IDictionary<string, DirectoryEntry> _virtualDirectories = null;
    private IDictionary<string, DirectoryEntry> virtualDirectories
    {
        get
        {
            if (_virtualDirectories == null)
            {
                _virtualDirectories = new Dictionary<string, DirectoryEntry>();

                DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName);
                foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
                {
                    _virtualDirectories.Add(virtualDirectory.Name, virtualDirectory);
                }
            }
            return _virtualDirectories;
        }
    }

List all virtual directories in IIS 5,6 and 7