In my application (Windows Forms) I have to make a directory tree for the user to search for one (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 the 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 (retrieve the FileName from them) for my application.
Follow 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 the node and display it in the ListView, but how do I add it to TreeView?
Thank you.