目录结构中的C#build菜单

时间:2012-05-08 11:34:01

标签: c# winforms

我目前有以下问题。我有一个像

这样的目录结构
root
 - level 1
   - level 1.1
     - level 1.2
 - level 2
   - level 2.1
 - level 3
 - level 4
   - level 4.1

从此我想建立一个菜单。所以root将是要点击的菜单项,并且需要所有级别来深入查看您想要获得的信息。

由于我对C#(不编程)很陌生,我想知道.NET是否对此任务有任何帮助。我不想开始使用已经存在的代码...

感谢您的任何意见!

3 个答案:

答案 0 :(得分:2)

您可以使用DirectoryInfo类获取给定根文件夹的所有子目录的列表。您应该对子目录执行递归搜索,并使用该数据构建菜单。

以下是一些代码将为您完成这项工作,它假设您已经有一个名为menuStrip1的MenuStrip:

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;
}

别忘了更改根文件夹路径!


注意:Yorye Nathan对快捷文件夹提出了一个很好的观点。如果您的任何文件夹是父文件夹的快捷方式,这将导致无限循环。解决此问题的最简单方法是确保您的结构不包含任何捷径。假设您具有针对此应用程序的专门构建的结构,这可能是一个简单的选择。但是,如果您在用户定义的根文件夹上运行它,则需要检查这些。

您可以修改GetMenuItem函数,如下所示,假设.Net 3.5或更高版本(LINQ +可选参数):

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;
}

答案 1 :(得分:1)

已编辑现在支持递归文件夹(忽略以防止无限循环)

public static MenuStrip CreateMenu(string rootDirectoryPath)
{
    var dir = new DirectoryInfo(rootDirectoryPath);

    var menu = new MenuStrip();
    var root = new ToolStripMenuItem(dir.Name);

    var includedDirs = new List<string> {dir};

    menu.Items.Add(root);

    AddItems(root, dir, includedDirs);

    return menu;
}

private static void AddItems(ToolStripDropDownItem parent, DirectoryInfo dir, ICollection<string> includedDirs)
{
    foreach (var subDir in dir.GetDirectories().Where(subDir => !includedDirs.Contains(subDir.FullName)))
    {
        includedDirs.Add(subDir.FullName);
        AddItems((ToolStripMenuItem)parent.DropDownItems.Add(subDir.Name), subDir, includedDirs);
    }
}

答案 2 :(得分:0)