简体   繁体   English

将元素添加到列表的递归方法

[英]Recursive method that adds elements to list

I would like to get the list of all sub-directories of a folder, including the nested ones. 我想获取一个文件夹的所有子目录的列表,包括嵌套的目录。
Therefore, I would like to have a list<Folder> myList that will be filled by a recursive method. 因此,我希望有一个list<Folder> myList将由递归方法填充。

 void myMethod(?)  
 {
   list<Folder> listFolders = ...  ;
   foreach (Folder curFolder in listFolders)
      {
        myList.add(curFolder);
      }
 }

Is it possible? 可能吗?

Declare a function like this 声明这样的功能

List<Folder> ListFolders(Folder initialFolder)
{
    var folders = new List<Folder>();
    folder.Add(initialFolder);
    foreach (var f in initialFolder.GetSubFolders())
    {
        folders.AddRange(ListFolders(f));
    }
    return folders;
}

The Folder class is left to you as an exercice, it will have to contain the name of the current folder, and a method to get its subfolders (hint: look into the System.IO.Directory class) Folder类作为练习留给您使用,它必须包含当前文件夹的名称以及获取其子文件夹的方法(提示:查看System.IO.Directory类)

The following is an extension method that selects from an IEnumerable Recursively: 以下是一种从IEnumerable递归选择的扩展方法:

    public static IEnumerable<T> SelectRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> recursiveSelector)
        {
            foreach (var i in source)
            {
                yield return i;

                var directChildren = recursiveSelector(i);
                var allChildren = SelectRecursive(directChildren, recursiveSelector);

                foreach (var c in allChildren)
                {
                    yield return c;
                }
            }
        }

You can use it to create a "flat" enumerable out of every hierarchy. 您可以使用它在每个层次结构中创建一个“可枚举”的枚举。 Assume the Folder class has a property that returns its direct children: as follows: 假设Folder类具有一个返回其直接子级的属性:

public class Folder 
{
    public IEnumerable<Folder> Children {get;}
}

You will be able to get a flat list of sub folders with the following line: 您将能够获得带有以下行的子文件夹的平面列表:

var flatList = folder.SelectRecursive(f => f.Children).ToList();

Hope that this is what you mean. 希望这就是你的意思。

    static void Main(string[] args)
    {
            List<string> dirs = new List<string>();
            addDirectoryToList(@"C:\exampledir\", dirs);
    }

    void addDirectoryToList(string dir, List<string> list)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(dir);
        foreach (DirectoryInfo subdir in dirInfo.GetDirectories())
        {
            addDirectoryToList(subdir.FullName, list);
        }
    }

or without recursion: 或没有递归:

    void addDirectoryToList(string dir, List<string> list)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(dir);
        list.Add(dir);
        foreach (DirectoryInfo subdir in dirInfo.GetDirectories("*", SearchOption.AllDirectories))
        {
            list.Add(subdir.FullName);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM