Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TreeView where the root node is the primary drive on my computer. I can list all the names of the folders inside that drive with the code below whenever the root node is clicked. The root node is the only node that cannot have a parent. So when a node is clicked, I need to check first if it has a parent, if it has a parent, I use a loop to get the text of all the parents up to the root node while I add the text of the clicked child node and the parents to a list. I then use a join method to form a path that can be used to get all the directories in that child node folder. Which conditional structure is the right one to use? a while loop or a do while, my code is shown below.

What I have tried:

<pre lang="C#">

   private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //get the name of the parent drive
            var parent = e.Node.Text;
            if (e.Node.Parent != null)
            {
                MessageBox.Show(e.Node.Parent.Text);
                var path = new List <string>(); 
                var node = e.Node;
                path.Add(node.Text);    
                while (node.Parent != null)
                {
                    MessageBox.Show(node.Text);
                    //add the text to the list
                    path.Add(node.Text);
                    node = node.Parent;
                }
                var final = string.Join("\\", path);
                MessageBox.Show(final);
            }
            if (e.Node.IsExpanded)
            {
                //collpase the node
                //e.Node.Collapse();

            }
            else
            {
                //get all directories in the drive
                if(e.Node.Parent != null)
                {
                    var node = e.Node.Parent;
                    while (node.Parent != null)
                    {
                        MessageBox.Show(node.Text);
                        node = node.Parent;
                    }
                 
                }
                else
                {
                    e.Node.Nodes.Clear();
                    List<DirectoryInfo> list = new List<DirectoryInfo>();
                    var files = Directory.GetDirectories(parent);
                    files.ToList().ForEach(item => list.Add(new DirectoryInfo(item)));
                    list.ForEach(item => e.Node.Nodes.Add(item.Name));
                }
             
            }
        }
Posted
Updated 30-May-23 22:02pm

In case you are asking about this loop:

if(e.Node.Parent != null)
{
    var node = e.Node.Parent;
    while (node.Parent != null)
    {
        MessageBox.Show(node.Text);
        node = node.Parent;
    }


I think you can simplify it like this:

var node = e.Node;
while (node.Parent != null)
{
    node = node.Parent;
    MessageBox.Show(node.Text);
}
 
Share this answer
 
Comments
Tim the Gamer 31-May-23 4:07am    
You have moved the reset code to the top of the Message Box statement, let me check if it outputs the nodes parents text as expected.
The choice between while and do..while is fairly simple: if you want to test the condition before executing the code, then use while; if you want to execute the code in the loop at least once before testing the condition, then use do..while.

The bigger problem is your choice of collection. By appending each parent node's text to a List<string>, you'll end up with the path in the wrong order. For example, if you have a tree structure of C: ⇒ Users ⇒ Tim ⇒ Documents, your final path will be: "Documents\Tim\Users\C:".

You could replace the Add call with an Insert call:
C#
path.Add(node.Text);    
while (node.Parent != null)
{
    path.Insert(0, node.Text);
    node = node.Parent;
}
Or you could switch to using a Stack<string>, which iterates its values in the reverse order, producing the correct results:
C#
var path = new Stack<string>(); 
var node = e.Node;
while (node != null)
{
    path.Push(node.Text);
    node = node.Parent;
}
string final = string.Join(Path.DirectorySeparatorChar, path);
Stack<T> Class (System.Collections.Generic) | Microsoft Learn[^]
 
Share this answer
 
v2
Comments
Tim the Gamer 31-May-23 4:08am    
Yes because I can reverse the order of the array and have the full path to the folder unless that is not optimizing the program for the CPU
Tim the Gamer 31-May-23 4:09am    
I got a good feeling about this, can't wait to try it out.
Maciej Los 31-May-23 12:38pm    
5ed!
Tim the Gamer 31-May-23 12:39pm    
This worked and I accepted it as an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900