循环遍历 DirectoryEntry 或任何对象层次结构 - C#

发布于 2024-07-16 00:26:43 字数 557 浏览 9 评论 0原文

我目前正在开发一个应用程序,该应用程序使用 System.DirectoryServices 命名空间创建 DirectoryEntry 对象并循环整个层次结构以收集信息。

我不知道层次结构中每个 DirectoryEntry 对象的子条目数,因此我无法通过 Children 属性为蜘蛛创建 N 个嵌套循环

这是我的伪代码示例:

//root directory
DirectoryEntry root = new DirectoryEntry(path);

if(DirectoryEntry.Childern != null)
{
    foreach(DirectoryEntry child in root.Children)
    {
        //loop through each Children property unitl I reach the last sub directory
    }
}

我的问题是,最好的方法是什么如果您不知道对象中的子目录数量,请创建一个循环来收集信息?

(这可以应用于您不知道对象层次结构的任何类型的对象)

I am currently developing an application that use the System.DirectoryServices namespace to create a DirectoryEntry object and loop through the entire hierarchy to collect information.

I do not know number of child entries for each DirectoryEntry object in the hierarchy, so I can not create a N number of nested loops to spiders through the Children property

Here is my pseudo code example:

//root directory
DirectoryEntry root = new DirectoryEntry(path);

if(DirectoryEntry.Childern != null)
{
    foreach(DirectoryEntry child in root.Children)
    {
        //loop through each Children property unitl I reach the last sub directory
    }
}

My question is, what is the best way to create a loop to collect information if you so not know the number of sub directories in your object?

(This can be applied to any type of object that you do not know the object hierarchy)

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

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

发布评论

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

评论(5

北凤男飞 2024-07-23 00:26:43

如果您不知道层次结构的深度并且需要向下遍历所有级别,请使用递归函数。
下面是使用深度优先遍历的示例。

using (DirectoryEntry root = new DirectoryEntry(someDN))
{
    DoSomething(root);
}


function DoSomething(DirectoryEntry de)
{
    // Do some work here against the directory entry

    if (de.Children != null)
    {
        foreach (DirectoryEntry child in de.Children)
        {
            using (child)
            {
                DoSomething(child);
            }
        }
    }
}

或者,如果没有递归,您可以通过添加队列或堆栈数据结构并存储您已经看到但尚未访问的对象来进行遍历。

Queue<DirectoryEntry> queue = new Queue<DirectoryEntry>();
DirectoryEntry root = new DirectoryEntry(someDN);
queue.Add(root);

while (queue.Any())
{
    using (DirectoryEntry de = queue.Dequeue())
    {
        // Do some work here against the directory entry

        if (de.Children != null)
        {
            foreach (DirectoryEntry child in de.Children)
            {
                queue.Enqueue(child);
            }
        }
    }
}

Use a recursive function if you don't know the depth of the hierarchy and need to traverse down through all levels.
Below is an example using depth-first traversal.

using (DirectoryEntry root = new DirectoryEntry(someDN))
{
    DoSomething(root);
}


function DoSomething(DirectoryEntry de)
{
    // Do some work here against the directory entry

    if (de.Children != null)
    {
        foreach (DirectoryEntry child in de.Children)
        {
            using (child)
            {
                DoSomething(child);
            }
        }
    }
}

Alternatively, without recursion, you can do a traversal by adding a Queue or Stack data structure and storing the objects you've seen but havent visited yet.

Queue<DirectoryEntry> queue = new Queue<DirectoryEntry>();
DirectoryEntry root = new DirectoryEntry(someDN);
queue.Add(root);

while (queue.Any())
{
    using (DirectoryEntry de = queue.Dequeue())
    {
        // Do some work here against the directory entry

        if (de.Children != null)
        {
            foreach (DirectoryEntry child in de.Children)
            {
                queue.Enqueue(child);
            }
        }
    }
}
你丑哭了我 2024-07-23 00:26:43

您必须将递归函数编写为...

DirectoryEntry root = new DirectoryEntry(path);
DoForEveryNode(root);

void DoForEveryNode(DirectoryEntry node)
{
    // do something..

    foreach(DirectoryEntry child in node.Children)
    {
        DoForEveryNode(child);
    }
}

You have to write recursive function as...

DirectoryEntry root = new DirectoryEntry(path);
DoForEveryNode(root);

void DoForEveryNode(DirectoryEntry node)
{
    // do something..

    foreach(DirectoryEntry child in node.Children)
    {
        DoForEveryNode(child);
    }
}
心房的律动 2024-07-23 00:26:43

您可以使用一个在子级上递归调用自身的函数。
退出条件:不再有孩子等。

You could use a function which recursively calls itself on the children.
exit condition: no more children etc..

笑饮青盏花 2024-07-23 00:26:43

一种选择是使用递归。 在一个函数中设置该代码,然后该函数在 foreach 循环内调用自身,每次传递下一个目录(子项)

One option is to use Recursion. Set that code up in a function that then calls itself inside the foreach loop, passing the next directory (child item) each time

纸短情长 2024-07-23 00:26:43

欢迎来到奇妙的递归世界。 您需要一个接受 Directory 作为参数的函数。 给定该目录,它会查找所有子目录,并为每个子目录...调用自身。

Welcome to the wonderful world of recursion. You need a function that accepts a Directory as an argument. Given that directory, it looks up all of the child directories and for each one... calls itself.

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