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

发布于 2024-10-16 04:20:12 字数 1142 浏览 6 评论 0原文

我尝试以编程方式在 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 = "Folder 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;
        }

这对我来说相当难看。有没有更好的方法可以检查现有虚拟目录(如果它们存在于子文件夹中)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-10-23 04:20:12

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

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文