Question Omit Parent Node in Treeview

Jalkhov

New member
Joined
Mar 24, 2021
Messages
3
Programming Experience
3-5
I have the following code
C#:
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;
}
I got from here. It is to pass a directory to a Treeview object, it works fine and gives as a result something like this:

└───Main folder ├───One folder │ ├───Config.example │ ├───DB │ │ └───example.sqlite │ └───Style │ └───estyles.example ├───Other folder └───Another folder

I would like to know how I can prevent the parent node, in this case Main folder, from being shown, and how to keep it like this:

One folder ├───Config.example ├───DB │ └───example.sqlite └───Style └───estyles.example Other folder Another folder

Basically I only want to display the contents of the directory in question.
 
Last edited by a moderator:
Solution
Stop thinking in code and start thinking in logic. Work out the logic first and then write code to implement that logic. How can you possibly write code effectively if you don't know what that code has to do? You can't, so think about what the code has to do first, then write the code to do it. I'm not just talking about the end result but rather the steps to get there. You've not shown that you have actually considered that at all.

What logic does the code that you already have implement? You have a method that takes in a folder, creates a node for that folder, recursively calls that method for each subfolder and then creates a node for each file. What's the logic you actually want to implement? You don't even know because you haven't...

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,756
Location
Sydney, Australia
Programming Experience
10+
Stop thinking in code and start thinking in logic. Work out the logic first and then write code to implement that logic. How can you possibly write code effectively if you don't know what that code has to do? You can't, so think about what the code has to do first, then write the code to do it. I'm not just talking about the end result but rather the steps to get there. You've not shown that you have actually considered that at all.

What logic does the code that you already have implement? You have a method that takes in a folder, creates a node for that folder, recursively calls that method for each subfolder and then creates a node for each file. What's the logic you actually want to implement? You don't even know because you haven't actually thought about it. You want a method that takes in a folder, creates a node and recursively calls the method for each subfolder and then creates a node for each file. You already know how to do all those things because you're already doing them, so you can write the code for yourself because you now know what the code actually has to do.
 
Solution

Jalkhov

New member
Joined
Mar 24, 2021
Messages
3
Programming Experience
3-5
I have managed to solve it by scrutinizing the code, the truth I must say thank you, however I must also say that if you had not added that bit of arrogance the advice would have been wonderful, yes, you are an experienced programmer and methodology, but not why you should treat those who are starting as naive, I understood perfectly what you mean, but it is not like I had not done anything to solve it on my own, so thanks again, but for next advice, a little less arrogance. You may think that the rudeness you speak helps people, and maybe it works to some extent, but as a professional you should also address people better.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,756
Location
Sydney, Australia
Programming Experience
10+
as a professional you should also address people better.
Unless you're paying me, I'm here as a volunteer, not a professional.

I tend to "speak" quite bluntly in these forums. People get a bit sensitive because they assume that criticism will not be part of the help they receive but nowhere did I suggest that you're a bad person for doing things the wrong way, that it's not so common as to be normal for beginners to do things the wrong way or that I wasn't prone to doing similar things as a beginner myself. Any offence you're feeling is inferred, not implied.
 
Top Bottom