Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:38:41+00:00 2026-05-23T01:38:41+00:00

i am new with Nodes here.. :) i came up with this algorithm but

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:38:42+00:00Added an answer on May 23, 2026 at 1:38 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: root.Nodes.Add(new TNode() { Foo1 = bar1, Foo2 = bar2, Foo3 = bar3
I'm trying to add a new node to an jQuery SimpleTree , but all
New to javascript/jquery and having a hard time with using this or $(this) to
New to silverlight. Traditionally if I were to design a wizard-like process where a
New to both Ruby and Rails but I'm book educated by now (which apparently
New to WCF, but familiar with COM+ - can I wrap a WCF service
We currently have a failover sql cluster with two nodes. For a new large
Here is a java code for breadth-first travel: void breadthFirstNonRecursive(){ Queue<Node> queue = new
I have a html string like this: <html><body><p>foo <a href='http://www.example.com'>bar</a> baz</p></body></html> I wish to
I am relatively new to Java and while trying out some code came across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.