34

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);
}
Andy G
  • 19,232
  • 5
  • 47
  • 69
Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85
  • Hey @Vincent Dagpin can you show your solution for your project ? I am working on the exact same thing as you do but I can't find any video or posting which provides a template - your question is the only thing that has something to do with my current project. I would really appreciate it :) – Burak Sep 06 '22 at 13:43

1 Answers1

118

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);
}
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • 5
    @vrynxzent - if you are interested, updated the answer with non-recursive approach too. – Alex Aza Jun 04 '11 at 22:12
  • 1
    ahh.. i see.. i prefer using the non-recursive because of threading and try catch.. i can do it also in recursive method but i need to see the form that updating the data while fetching files and directories.. – Vincent Dagpin Jun 04 '11 at 23:32
  • 1
    I used both recursive and iterative functions and this is my code, but the compler returns an error: **Access to the path 'E:\System Volume Information\' is denied.**; this is my code: **ListDirectory(treeView1,@"E:\");** –  Sep 12 '12 at 19:12
  • 6
    Use a try-catch for UnauthorizedAccessException. – jhoanna Sep 28 '12 at 03:07
  • 2
    I was getting an exception with currentNode.Nodes.Add(). I changed that to currentNode.ChildNodes.Add() and it works – Mark B Apr 03 '13 at 23:28
  • 1
    @Mark B fyi: there is no ChildNodes Property in the WinForm TreeView – BillW Apr 12 '17 at 10:40
  • How can i omit the Main folder node and show ONLY the folder content? – Jalkhov Mar 24 '21 at 13:08