看代码
一、调用时主函数
//using System.IO;
//前面略..
dirInfo = new DirectoryInfo(path);
List<FileInfo[]> list = new List<FileInfo[]>();
//获得 文件列表"数组"的 "集合",相当于交错数组
list = this.GetListFileInfos(dirInfo,0,2);
二、带控制的递归函数
//递归算法
// private int depthCount;
private List<FileInfo[]> GetListFileInfos(DirectoryInfo dirInfo,int currentDepth, int depth)
{
List<FileInfo[]> list = new List<FileInfo[]>();
DirectoryInfo[ ] dir = dirInfo.GetDirectories();
currentDepth = currentDepth + 1;
if (currentDepth < depth)
{ //------注意(1):调用自身是控制递归处
foreach (var item in dir)
{
list.AddRange(this.GetListFileInfos(item, currentDepth, depth));
}
}
//else
//{------注意(2)无须,如果 设置错误的话,执行一次也返回.
// throw new Exception("深度应该当大于当前深度");
// // return null;
//}
FileInfo[] fileInfos = dirInfo.GetFiles();
list.Add(fileInfos);
return list;
}