使用文件系统目录结构填充 TreeView

发布于 2024-11-13 22:29:40 字数 1588 浏览 3 评论 0原文

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

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

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

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

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

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

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

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

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

发布评论

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

评论(2

你曾走过我的故事 2024-11-20 22:29:40

选项#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);
}

Option #1: Recursive approach:

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

Option #2: Non-recursive approach:

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);
}
无妨# 2024-11-20 22:29:40

从@Alex Aza 的非递归答案开始......

    private static void AddDirectoryNode(TreeView pTreeView, string pPath) {
   Stack<MyNode>? stack = new Stack<MyNode>();
   DirectoryInfo? rootDirectory = new DirectoryInfo(pPath);
   MyNode? node = new MyNode(rootDirectory.FullName);

   TreeNodeCollection myNodeCollection = pTreeView.Nodes;
   for (int i = (myNodeCollection.Count - 1); i != -1; i--) {
      if (string.Equals(((MyNode)myNodeCollection[i]).mPath, pPath, StringComparison.OrdinalIgnoreCase))
         pTreeView.Nodes.Remove(myNodeCollection[i]);
   }
   stack.Push(node);
   while (stack.Count > 0) {
      MyNode? currentNode = stack.Pop();
      try {
         DirectoryInfo? directoryInfo = new DirectoryInfo(currentNode.mPath);

         foreach (DirectoryInfo directory in directoryInfo.GetDirectories()) {
            MyNode? childDirectoryNode = new MyNode(directory.FullName);

            currentNode.Nodes.Add(childDirectoryNode);
            stack.Push(childDirectoryNode);
         }
         foreach (FileInfo file in directoryInfo.GetFiles())
            currentNode.Nodes.Add(new MyNode(file.FullName));
      }
      catch (UnauthorizedAccessException) {
         //efm5 Just ignore them
      }
      catch (Exception pException) {
         _ = sForm.AskingAsync(new TM("", pException));
      }
   }
   pTreeView.Nodes.Add(node);
}

MyNode 是一个基于 TreeNode 构建的类;它的成员 mPath 包含完整路径。请原谅捕获的“AskingAsync…”消息处理——只需将它们视为非常奇特的消息框即可。

这样做的一件事是消除重复的初始节点。

Starting with @Alex Aza’s non-recursive answer…

    private static void AddDirectoryNode(TreeView pTreeView, string pPath) {
   Stack<MyNode>? stack = new Stack<MyNode>();
   DirectoryInfo? rootDirectory = new DirectoryInfo(pPath);
   MyNode? node = new MyNode(rootDirectory.FullName);

   TreeNodeCollection myNodeCollection = pTreeView.Nodes;
   for (int i = (myNodeCollection.Count - 1); i != -1; i--) {
      if (string.Equals(((MyNode)myNodeCollection[i]).mPath, pPath, StringComparison.OrdinalIgnoreCase))
         pTreeView.Nodes.Remove(myNodeCollection[i]);
   }
   stack.Push(node);
   while (stack.Count > 0) {
      MyNode? currentNode = stack.Pop();
      try {
         DirectoryInfo? directoryInfo = new DirectoryInfo(currentNode.mPath);

         foreach (DirectoryInfo directory in directoryInfo.GetDirectories()) {
            MyNode? childDirectoryNode = new MyNode(directory.FullName);

            currentNode.Nodes.Add(childDirectoryNode);
            stack.Push(childDirectoryNode);
         }
         foreach (FileInfo file in directoryInfo.GetFiles())
            currentNode.Nodes.Add(new MyNode(file.FullName));
      }
      catch (UnauthorizedAccessException) {
         //efm5 Just ignore them
      }
      catch (Exception pException) {
         _ = sForm.AskingAsync(new TM("", pException));
      }
   }
   pTreeView.Nodes.Add(node);
}

MyNode is a class built on TreeNode; its member mPath contains the full path. Please pardon the “AskingAsync…” message handling for the catches – just think of them as very fancy MessageBoxes.

One thing this does is eliminate the duplicate initial node.

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