The tag to the file anode is causing errors with the directory anode, is this because they are one and the same? Also i think That the casting from the tag to a fileinfo object isn't working. Any suggestions as to how to get the fileinfo into the nodes that the user selects to populate a listview?
//see code below where attempts made to add tags
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "Folder";
aNode.ImageIndex = 0;
aNode.SelectedImageIndex = 1;
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
//add files to treeview
foreach (var file in subDir.GetFiles())
{
if(file.Name.Contains(".rfa"))
{
aNode.Nodes.Add(new TreeNode(file.Name));
//cant add a tag to the file only to the directory
aNode.Tag = file;
}
}
nodeToAddTo.Nodes.Add(aNode);
}
1) In order to check the file's extension, it's a better practice to use:
2) You're modifying the aNode 'Tag' property, which is probably what causing your problem, try modifying it as follows:
3) IMHO, you should not keep the FileInfo/DirectoryInfo object in the 'Tag', rather use the path itself
4) If you simply want to display the directory's structure in a treeview, look at the following: https://stackoverflow.com/a/6239644/120391 [You can easily modify it to display only '.rfa' files]