简体   繁体   中英

Creating virtual directories in sub directories in IIS 6.0 (programmatically)

I am attempting to create virtual directories in IIS 6.0 programmatically and having problems checking if a virtual directory exists if the virtual directory is in a nested folder.

So if I have a folder tree such as:

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

When I grab the DirectoryEntry object for this site metabase:

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

The DirectoryEntry object (will call it entry) will have two children, with

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

but entry.Children[1].Name = "Folder B" which is not a virtual directory. I have to do the following (code) to get to any virtual directories in nested folders:

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;
        }

Which to me is quite ugly. Is there a better way that I can check for existing virtual directories if they exist in sub folders?

Are you looking for a recursive function to iterate the complete site?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM