遍历文件夹树,自下而上

发布于 2024-09-13 06:54:57 字数 187 浏览 0 评论 0原文

我正在寻找使用 C#/.NET 3.5 快速有效地自下而上遍历文件夹的想法,

例如:

-0
--1
---12
--2
---21
---22
---23

第一次步行:12,21,22,23 然后:1,2

谢谢

I'm looking for ideas for fast and effective way to walk through folders bottom-up using C#/.NET 3.5

for example:

-0
--1
---12
--2
---21
---22
---23

first walk though: 12,21,22,23
then: 1,2

Thanks

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

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

发布评论

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

评论(2

人间☆小暴躁 2024-09-20 06:54:57

以通常的方式遍历树,并将包含每个节点和节点级别的对象添加到 IEnumerable

public class DirectoryNode
{
    public DirectoryInfo Dir { get; set; }
    public int Level { get; set; }
}

public IEnumerable<DirectoryNode> myNodes;

然后,要获取列表,只需使用 OrderByDescending linq 扩展方法调用生成的 IEnumerable,如下所示:

var result = myNodes.OrderByDescending(node => node.Level);

Walk the tree the usual way, and add an object containing each node and the level of the node to an IEnumerable.

public class DirectoryNode
{
    public DirectoryInfo Dir { get; set; }
    public int Level { get; set; }
}

public IEnumerable<DirectoryNode> myNodes;

Then, to get your list, just call your resulting IEnumerable with the OrderByDescending linq extension method, like this:

var result = myNodes.OrderByDescending(node => node.Level);
阳光①夏 2024-09-20 06:54:57

您需要每个节点的深度,并且在开始遍历树之前需要找到所有节点的最大深度。因此,您需要以前序、中序或后序遍历的方式遍历所有节点,找到它们的深度,然后以与深度相反的顺序再次遍历整个树。

You need the depth of each node and you need to find the maximum depth of all nodes before starting to traverse the tree. So you need to traverse all nodes in a preorder, inorder, or postorder traversal, find their depths, and then traverse the whole tree again in reverse order of depth.

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