Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:50:16+00:00 2026-06-02T11:50:16+00:00

I am working on a simple, portable, single-form, application where the *.exe and a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:50:17+00:00Added an answer on June 2, 2026 at 11:50 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple 'Working' form that runs on its own thread to keep
Currently i am working in Simple game application, i have a doubt on glVertexAttribPointer
I'm working with my simple PHP captcha algorithm ( http://www.source.ofitall.com/devel/captcha.php ) and I have
I'm working on Windows XP I have portable version of Eclipse Galileo, but I
I have a simple working PHP script to write an HTML table from a
I am working in simple application in which I need to post data to
I am working on simple application which creates google calendar (under it's own account).
I'm new to Objective-C and I have been working with simple programs, and I
I am trying to convert a working simple Java application to an applet. The
I have been working with simple linq2sql statements for a bit but am new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.