![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void Main(string[] args) 2 { 3 4 5 Traversing("C:\\Documents and Settings\\Administrator\\桌面\\京广高速\\广州南站\\无交叉线岔\\"); 6 Console.ReadKey(); 7 } 8 /// <summary> 9 /// 几乎最简练的文件夹遍历方法(8行代码,广度优先算法) 10 /// </summary> 11 /// <param name="sPathName">起始文件夹</param> 12 public static void Traversing(string sPathName) 13 { 14 //创建一个队列用于保存子目录 15 Queue<string> pathQueue = new Queue<string>(); 16 //首先把根目录排入队中 17 pathQueue.Enqueue(sPathName); 18 //开始循环查找文件,直到队列中无任何子目录 19 while (pathQueue.Count > 0) 20 { 21 //从队列中取出一个目录,把该目录下的所有子目录排入队中 22 DirectoryInfo diParent = new DirectoryInfo(pathQueue.Dequeue()); 23 foreach (DirectoryInfo diChild in diParent.GetDirectories()) 24 pathQueue.Enqueue(diChild.FullName); 25 //查找该目录下的所有文件,依次处理 26 foreach (FileInfo fi in diParent.GetFiles()) 27 Console.WriteLine(fi.FullName); 28 } 29 }