The Thing
The Thing

Reputation: 635

TreeView Where Root And All Nodes Thereafter Are Derived From First Sub-Folder - How To Specify Correct Path?

I am working on a simple, portable, single-form, application where the *.exe and a folder entitled UserGeneratedContent reside in the same directory. On my form I have a treeview that I want to populate with nodes corresponding to all sub-folders (if any exist) of UserGeneratedContent. The code I've pasted in below works almost the way I want it to, but it populates the treeview with UserGeneratedContent as the root node and in turn treats the sub-folders it finds as child nodes, etc.

string folder = @"UserGeneratedContent";
FolderHierachy.Nodes.Add(GetDirectoryNodes(folder));

private static TreeNode GetDirectoryNodes(string path)
{
    var node = new TreeNode(Path.GetFileName(path));
    var subDirs = Directory.GetDirectories(path).Select(d => GetDirectoryNodes(d)).ToArray();

     node.Nodes.AddRange(subDirs);
     return node;
}

At this stage I have tried just about every combination of @"UserGeneratedContent" I can think of - adding back-slashes etc, in an attempt to access the sub-folders but I'm having no luck. I know its probably something simple, but I'm stuck - what is the correct syntax to ensure that the method above looks into UserGeneratedContent to get the sub-folders?

Furthermore, I want the treeview control to allow the user to create, rename, and delete folders in any hierachy of their choice so I'm wondering what would be considered best practices in this regard?

Edit \ Update # 1 - 12.2.2012:

After a lot more trial and error I have given up on the code above - no matter what I tried I couldn't get it to work for some reason. Luckily the code below is working perfectly for me :-)

DirectoryInfo dirInfo = new DirectoryInfo(@"UserGeneratedContent");
DirectoryInfo[] subDirs = dirInfo.GetDirectories();

string pathToSubFolders = Path.Combine(@"UserGeneratedContent", subDirs[0].ToString());

PopulateTreeView(treeView1, pathToSubFolders);

private void PopulateTreeView(TreeView treeView, string path)
{
     treeView.Nodes.Clear();
     var rootDirectoryInfo = new DirectoryInfo(path);
     treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
     var directoryNode = new TreeNode(directoryInfo.Name);           
     foreach(var directory in directoryInfo.GetDirectories())
     {
          directoryNode.Nodes.Add(CreateDirectoryNode(directory));
     }

     return directory;
}

I need to add some error handling code to it to allow for there being no sub-folders under @"UserGeneratedContent" - as it is subDirs[0].ToString() throws an IndexOutOfRangeException if no sub-folders exist and I'm not exactly sure how to go about it so I'd be grateful for some pointers.

Edit \ Update # 2 - 12.2.2012:

For my error checking I've used a try-catch block as below:

try
{

  string pathToSubFolders = Path.Combine(dirInfo.ToString(), subDirs[0].ToString());

  PopulateTreeView(treeView1, pathToSubFolders);

}
catch (IndexOutOfRangeException)
{

            //Do Something Here

}

The above code works, but can anyone tell me if I have gone about this the right way?

Thanks for reading.

Upvotes: 0

Views: 1272

Answers (1)

FriendlyGuest
FriendlyGuest

Reputation: 46

if you haven't found an other solution yet, a simple way would be:

if(subDirs.Lenght != 0)        // or > 0
{
  string pathToSubFolders = Path.Combine(dirInfo.ToString(), subDirs[0].ToString());
  PopulateTreeView(treeView1, pathToSubFolders);
}
else
{
  //Do Something Here
}

Upvotes: 2

Related Questions