在IIS 6.0中的子目录中创建虚拟目录(以编程方式)

时间:2011-02-05 16:37:30

标签: c# iis-6 directoryentry

我正在尝试以编程方式在IIS 6.0中创建虚拟目录,如果虚拟目录位于嵌套文件夹中,则检查虚拟目录是否存在时会出现问题。

所以,如果我有一个文件夹树,如:

MySite
  Folder A (virtual directory)
  Folder B
      NestedFolder C (virtual directory)

当我获取此站点配置数据库的DirectoryEntry对象时:

"IIS://<servername>/W3SVC/2/Root"

DirectoryEntry对象(将其称为条目)将有两个孩子,

entry.Children[0].Name = "Folder A"

但是entry.Children [1] .Name =“文件夹B”,它不是虚拟目录。我必须执行以下(代码)才能访问嵌套文件夹中的任何虚拟目录:

foreach (var directoryEntry in entry.Children.Cast<DirectoryEntry>().Where(directoryEntry => directoryEntry.SchemaClassName == "IIsWebVirtualDir" || directoryEntry.SchemaClassName == "IIsWebDirectory")) {
            foreach (DirectoryEntry child in directoryEntry.Children.Cast<DirectoryEntry>().Where(subChild => subChild.SchemaClassName == "IIsWebVirtualDir")) {
                if (child.Name == vDir)
                    return true;
            }

            if (directoryEntry.Name != vDir) continue;
            return true;
        }

对我来说这很难看。如果存在于子文件夹中,是否有更好的方法可以检查现有的虚拟目录?

谢谢,

1 个答案:

答案 0 :(得分:1)

您是否在寻找递归函数来迭代整个网站?

public static void Main() {
    var siteRoot = new DirectoryEntry("IIS://<servername>/W3SVC/2/Root");
    var containsVirtualDirectory = ContainsVirtualDirectory(siteRoot);
}

private static Boolean ContainsVirtualDirectory(DirectoryEntry container) {
    foreach (DirectoryEntry child in container.Children) {
        if (child.SchemaClassName == "IIsWebVirtualDir")
            return true;

        if (child.SchemaClassName == "IIsWebDirectory" && ContainsVirtualDirectory(child))
            return true;
    }

    return false;
}