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

  • SEARCH
  • Home
  • 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 7899313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:40:08+00:00 2026-06-03T08:40:08+00:00

I have currently the following problem. I have a directory structure like root –

  • 0

I have currently the following problem. I have a directory structure like

root
 - level 1
   - level 1.1
     - level 1.2
 - level 2
   - level 2.1
 - level 3
 - level 4
   - level 4.1

from this I want to build a menu. so root will be the menu item to click on and all the level will be needed to drill down to the information you want to get.

As I’m pretty new to C# (not programming) I wanted to know if there is any help from .NET for this task. I don’t want to start to fiddel around with code that is already there…

Thanks for any input!

  • 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-03T08:40:10+00:00Added an answer on June 3, 2026 at 8:40 am

    You can use the DirectoryInfo class to obtain a list of all sub-directories for a given root folder. You should the perform a recursive search on sub-directories and build your menu using that data.

    Here is some code that will do the job for you, it assumes you already have a MenuStrip called menuStrip1:

    public void BuildMenu()
    {
        //first we get the DirectoryInfo for your root folder, this will be used to find the first set of sub-directories
        DirectoryInfo dir = new DirectoryInfo(@"C:\MyRootFolder\");//Change this
    
        //next we create the first MenuItem to represent the root folder, this is created using the GetMenuItem function
        ToolStripMenuItem root = GetMenuItem(dir);
    
        //we add our new root MenuItem to our MenuStrip control, at this point all sub-menu items will have been added using our recursive function
        menuStrip1.Items.Add(root);
    }
    
    public ToolStripMenuItem GetMenuItem(DirectoryInfo directory)
    {
        //first we create the MenuItem that will be return for this directory
        ToolStripMenuItem item = new ToolStripMenuItem(directory.Name);
    
        //next we loop all sub-directory of the current to build all child menu items
        foreach (DirectoryInfo dir in directory.GetDirectories())
        {
            item.DropDownItems.Add(GetMenuItem(dir));
        }
    
        //finally we return the populated menu item
        return item;
    }
    

    Dont forget to change the root folder path!


    NOTE: Yorye Nathan has made a good point about short-cut folders. If any of your folders is a short-cut to a parent folder this will cause an endless loop. The easiest way to solve this is to make sure your structure doesn’t contain any short-cuts. This may be an easy option for you assuming you have a specifically built structure for this application. If however, you are running this on a user-defined root folder you will want to check for these.

    You could modify the GetMenuItem function as below to account for this, assuming .Net 3.5 or higher (LINQ + optional parameters):

    public ToolStripMenuItem GetMenuItem(DirectoryInfo directory, List<DirectoryInfo> currentFolders = null)
    {
        if (currentFolders == null)
            currentFolders = new List<DirectoryInfo>();
    
        currentFolders.Add(directory);
    
        ToolStripMenuItem item = new ToolStripMenuItem(directory.Name);
    
        foreach (DirectoryInfo dir in directory.GetDirectories())
        {
            if (!currentFolders.Any(x => x.FullName == dir.FullName))//check to see if we already processed this folder (i.e. a unwanted shortcut)
            {
                item.DropDownItems.Add(GetMenuItem(dir, currentFolders));
            }
        }
    
        return item;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following directory structure. root --src ---tests src contains the source &
I have currently the following problem: The footers width gets smaller as soon as
I currently have the following problem with Apache Tapestry 5.3.1: The user should be
I have the following problem: I have a form in site/banen (currently local running
I am currently working on Problem 62 I have tried the following code to
Okay I have the following problem. I want to get the current dateTime and
I currently have the following SQL statement: SELECT s.first_name, s.last_name, tgs.group_identifier FROM staff s
I am currently struggling with the following problem. I have successfully installed Apache2 and
following is my files directory structure... Config config.php (is calling some files too like
I have an .Net MVC 3 web application that has the following structure root

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.