1

我目前有以下问题。我有一个像这样的目录结构

根
 - 1级
   - 1.1级
     - 1.2级
 - 2级
   - 2.1级
 - 3级
 - 4级
   - 4.1级

从这里我想建立一个菜单。所以 root 将是要单击的菜单项,并且需要所有级别来深入了解您想要获取的信息。

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

感谢您的任何意见!

4

3 回答 3

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 对快捷文件夹提出了很好的观点。如果您的任何文件夹是到父文件夹的快捷方式,这将导致无限循环。解决此问题的最简单方法是确保您的结构不包含任何快捷方式。假设您为此应用程序专门构建了结构,这对您来说可能是一个简单的选择。但是,如果您在用户定义的根文件夹上运行它,您将需要检查这些。

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

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;
}
于 2012-05-08T11:56:39.153 回答
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);
    }
}
于 2012-05-08T12:06:42.097 回答