使用文件系统目录结构填充 TreeView [英] Populate TreeView with file system directory structure

查看:38
本文介绍了使用文件系统目录结构填充 TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是节点的新手.. :) 我想出了这个算法,但它只显示父节点的列表..像这样..

i am new with Nodes here.. :) i came up with this algorithm but it only shows the list of parent nodes.. like this..

a
   a.txt
   b
   c
c
   m
   n
b
   o
   p
etc...

我希望下一个节点将被放置在前一个节点内的一个节点中..所以它会像这样出现..

i want the next node will be put in one of the node inside the previous node.. so it will come up like this..

a
   a.txt
   b
      o
      p
   c
      m
      n
etc...

<小时>

我有一些想法,但我可以将其实现到代码中.. :) 请提供任何帮助..


i have some ideas in mind but i can implement it to codes.. :) any help please..

private void ListDirectory(TreeView treeView, String path)
{            
    Stack<string> stack = new Stack<string>();
    TreeNode DirFilesCollection = new TreeNode();

    stack.Push(path);            

    while (stack.Count > 0)
    {
        string dir = stack.Pop();
        try
        {
            List<String> parentDir = new List<string>();
            parentDir.AddRange(Directory.GetFiles(dir, "*.*"));
            parentDir.AddRange(Directory.GetDirectories(dir));

            DirectoryInfo d = new DirectoryInfo(dir);
            TreeNode TParent = new TreeNode(d.Name);

            foreach (String s in parentDir)
            {
                FileInfo f = new FileInfo(s);
                TreeNode subItems = new TreeNode(f.Name);

                TParent.Nodes.Add(subItems);
            }

            DirFilesCollection.Nodes.Add(TParent);

            foreach (string dn in Directory.GetDirectories(dir))
            {
                stack.Push(dn);
            }
        }
        catch
        {}
    }

    Action clearTreeView = () => treeView.Nodes.Clear();
    this.Invoke(clearTreeView);

    Action showTreeView = () => treeView.Nodes.Add(DirFilesCollection);
    this.Invoke(showTreeView);
}

推荐答案

选项 #1:递归方法:

private void ListDirectory(TreeView treeView, string path)
{
    treeView.Nodes.Clear();
    var rootDirectoryInfo = new DirectoryInfo(path);
    treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
    var directoryNode = new TreeNode(directoryInfo.Name);
    foreach (var directory in directoryInfo.GetDirectories())
        directoryNode.Nodes.Add(CreateDirectoryNode(directory));
    foreach (var file in directoryInfo.GetFiles())
        directoryNode.Nodes.Add(new TreeNode(file.Name));
    return directoryNode;
}

选项 2:非递归方法:

private static void ListDirectory(TreeView treeView, string path)
{
    treeView.Nodes.Clear();

    var stack = new Stack<TreeNode>();
    var rootDirectory = new DirectoryInfo(path);
    var node = new TreeNode(rootDirectory.Name) { Tag = rootDirectory };
    stack.Push(node);

    while (stack.Count > 0)
    {
        var currentNode = stack.Pop();
        var directoryInfo = (DirectoryInfo)currentNode.Tag;
        foreach (var directory in directoryInfo.GetDirectories())
        {
            var childDirectoryNode = new TreeNode(directory.Name) { Tag = directory };
            currentNode.Nodes.Add(childDirectoryNode);
            stack.Push(childDirectoryNode);
        }
        foreach (var file in directoryInfo.GetFiles())
            currentNode.Nodes.Add(new TreeNode(file.Name));
    }

    treeView.Nodes.Add(node);
}

这篇关于使用文件系统目录结构填充 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆