CSharp遍历文件目录的递归与非递归实现

CSharp遍历文件目录的递归与非递归实现

遍历文件与文件夹的程序可以用递归实现,也可以用链表List,队列Queue,堆栈Stack。

详细代码如下。

一、添加类:FileAndFolder.cs

添加如下程序代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace SyncAndAsyncDemo

{

    ///

    /// 遍历所有文件和目录的递归 以及 非递归实现

    ///

    public class FileAndFolder

    {

        ///

        /// 使用递归Recursion实现

        ///

        ///

        public static void FindAllFiles_Recursion(string directoryPath)

        {

            string[] folderArray = Directory.GetDirectories(directoryPath);

            string[] fileArray = Directory.GetFiles(directoryPath);

            for (int i = 0; i < folderArray.Length; i++)

            {

                Console.WriteLine("目录:" + folderArray[i]);

                //继续遍历子目录

                FindAllFiles_Recursion(folderArray[i]);

            }

            for (int i = 0; i < fileArray.Length; i++)

            {

                Console.WriteLine("文件:" + fileArray[i]);

            }

        }

 

        ///

        /// 使用链表List实现

        ///

        ///

        public static void FindAllFiles_List(string directoryPath)

        {

            List<string> list = new List<string>();

            list.Add(directoryPath);

            while (list.Count > 0)

            {

                //移除list的第一个元素

                string path = list[0];

                list.RemoveAt(0);

                string[] folderArray = Directory.GetDirectories(path);

                string[] fileArray = Directory.GetFiles(path);

                for (int i = 0; i < folderArray.Length; i++)

                {

                    Console.WriteLine("目录:" + folderArray[i]);

                    //将最新的子目录放入集合中

                    list.Add(folderArray[i]);

                }

                for (int i = 0; i < fileArray.Length; i++)

                {

                    Console.WriteLine("文件:" + fileArray[i]);

                }

            }

        }

 

        ///

        /// 使用队列Queue实现

        ///

        ///

        public static void FindAllFiles_Queue(string directoryPath)

        {

            Queue<string> queue = new Queue<string>();

            queue.Enqueue(directoryPath);

            while (queue.Count > 0)

            {

                //移除list的第一个元素

                string path = queue.Dequeue();

                string[] folderArray = Directory.GetDirectories(path);

                string[] fileArray = Directory.GetFiles(path);

                for (int i = 0; i < folderArray.Length; i++)

                {

                    Console.WriteLine("目录:" + folderArray[i]);

                    //将最新的子目录放入集合中

                    queue.Enqueue(folderArray[i]);

                }

                for (int i = 0; i < fileArray.Length; i++)

                {

                    Console.WriteLine("文件:" + fileArray[i]);

                }

            }

        }

 

        ///

        /// 使用堆栈Stack实现

        ///

        ///

        public static void FindAllFiles_Stack(string directoryPath)

        {

            Stack<string> stack = new Stack<string>();

            stack.Push(directoryPath);

            while (stack.Count > 0)

            {

                string path = stack.Pop();

                string[] folderArray = Directory.GetDirectories(path);

                string[] fileArray = Directory.GetFiles(path);

                for (int i = 0; i < folderArray.Length; i++)

                {

                    Console.WriteLine("目录:" + folderArray[i]);

                    //将最新的子目录放入集合中

                    stack.Push(folderArray[i]);

                }

                for (int i = 0; i < fileArray.Length; i++)

                {

                    Console.WriteLine("文件:" + fileArray[i]);

                }

            }

        }

 

    }

}

二、在默认的Program.csMain函数增加如下测试代码:

        static void Main(string[] args)

        {

            string path = "D:\\data";

            Console.WriteLine("遍历文件使用递归实现:");

            FileAndFolder.FindAllFiles_Recursion(path);

 

            Console.WriteLine("遍历文件使用链表实现:");

            FileAndFolder.FindAllFiles_List(path);

 

            Console.WriteLine("遍历文件使用队列实现:");

            FileAndFolder.FindAllFiles_Queue(path);

 

            Console.WriteLine("遍历文件使用堆栈实现:");

            FileAndFolder.FindAllFiles_Stack(path);

            Console.ReadLine();

        }

三、程序运行效果:

注意:队列、堆栈的输出顺序是相反的【先进先出、后进先出】

你可能感兴趣的:(C#)