I am looking to change to name of the added node of a directory tree.
private static TreeNode GetDirectoryNodes(string path)
{
var dir = new DirectoryInfo(path);
var node = new TreeNode(path);
//node.Nodes.Add("Windows");
foreach (var directory in dir.GetDirectories())
{
node.Nodes.Add(GetDirectoryNodes(path + "\\" +directory.ToString()));
}
return node;
}
This will give an output like
C:\Test1
|
|-C:\Test1\Test1
| |-C:\Test1\Test1\Test1
|-C:\Test1\Test2
however I want to show
C:\Test1
|
|-Test1
| |-Test1
|-Test2
I have foung that if I use
foreach (var directory in dir.GetDirectories())
{
node.Nodes.Add(directory.ToString());
}
I will give the just the add path name but will not be recurrive for the sub directories output will be
C:\Test1
|
|-Test1
|-Test2
So how do I get the name to change
You can use DirectoryInfo.Name
for nodes, it will give you short name without full path (and if you need you can store FullName
in Tag). Like this:
var node = new TreeNode(dir.Name);