To be blunt: I don't understand your current design, and I don't have the time to put my head into your code. But, I would like to assist you, and I think the best way I can do that is to show you some working code I developed a few years ago.
1. Filling a TreeView recursively with every folder and sub-folder in a Drive:
a. requires: using System.IO;
b. assumes: a valid Drive Name in a string variable 'currentDrive
c. assumes a TreeView 'treeView1, and a ListView 'listView1.
d. the ListView has one column, and the View Property is set to 'Details
private string currentDrive = @"D://";
private DirectoryInfo currentDirectory;
private DirectoryInfo[] currentSubDirectories;
private TreeNode currentNode;
private void createTreeFromDrive()
{
currentDirectory = new DirectoryInfo(currentDrive);
currentSubDirectories = currentDirectory.GetDirectories();
treeView1.Nodes.Clear();
TreeNode currentNode = treeView1.Nodes.Add(currentDrive);
fillTree(currentNode, currentSubDirectories);
}
private void fillTree(TreeNode currentNode, DirectoryInfo[] currentDirectories)
{
foreach (var subDir in currentDirectories)
{
TreeNode subNode = currentNode.Nodes.Add(subDir.Name);
if (subDir.Name.Contains("WindowsImageBackup")) continue;
if (subDir.Name.Contains("System Volume Information")) continue;
if (subDir.Name.Contains("Recycle")) continue;
if (subDir.Name.Contains("Documents and Settings")) continue;
if (subDir.Name.Contains("inetpub")) continue;
DirectoryInfo[] subSubDirs = subDir.GetDirectories();
if(subSubDirs.Length > 0) buildTree(subNode, subSubDirs);
}
}
2. Given a click on a TreeNode, display the files in the associated Directory in a ListView:
a. assume you are using the default path separator string for a MS TreeView: the back-slash.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode currentNode = e.Node;
listView1.Items.Clear();
foreach (string file in Directory.EnumerateFiles(currentNode.FullPath, "*.*"))
{
listView1.Items.Add(file);
}
}
Comments: this code is rather old, except for the one revision to use .NET 4.0: if I were to re-do it today, I'd probably change some things.