1
In my application (Windows Forms) I have to make a tree of directories for the user to search for a (or more) file(s) within his system.
I saw on the Web how to make this tree in a Microsoft tutorial, but in their example Treeview only displays the directories, not the files themselves (the files are displayed in a Listview that is not practical).
The tree works, but I need to see the Directory files and if possible already select them (recover their Filename) for my application.
Follows the Code.
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"../..");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs,TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
I saw in this tutorial that I can apply a foreach to each file found on Node and display it in Listview, but how to add it to Treeview ?
Thank you.
You can take a look at this video : https://www.youtube.com/watch?v=55PCdfvlyYk it shows how you can display items in the Tree View control in the specified directory. Once I’m on time I’ll try to post the answer.
– stringnome
Thanks friend, I can not watch video at work, but I will take a look at this link when I get home, thanks so much for the help
– Guilherme Golfetto