public class Recursion
{
/// <summary>
/// 找出全部的子文件夹
/// </summary>
/// <param name="rootPath">根目录</param>
/// <returns></returns>
public static List<DirectoryInfo> GetAllDirectory(string rootPath)
{
List<DirectoryInfo> directoryList = new List<DirectoryInfo>();
DirectoryInfo directory = new DirectoryInfo(rootPath);
GetChildDirectory(directoryList, directory);
return directoryList;
}
/// <summary>
/// 找出某个文件夹的子文件夹,,放入集合
///
/// 递归:方法自身调用自身
/// </summary>
/// <param name="directoryList"></param>
/// <param name="parentDirectory"></param>
private static void GetChildDirectory(List<DirectoryInfo> directoryList, DirectoryInfo parentDirectory)
{
directoryList.AddRange(parentDirectory.GetDirectories());
if (parentDirectory.GetDirectories() != null && parentDirectory.GetDirectories().Length > 0)
{
foreach (var directory in parentDirectory.GetDirectories())
{
GetChildDirectory(directoryList, directory);
}
}
}
}