当先锋百科网

首页 1 2 3 4 5 6 7

List<FileInfo> result= new List<FileInfo>();
SearchFiles(@"E:\111",ref result);      

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABCSearchDirectory
{
    public class SearchDirectoryBase
    {
        private SearchDirectoryBase()
        {

        }
        /// <summary>
        /// 遍历指定文件夹中的文件包括子文件夹的文件
        /// </summary>
        /// <param name="filePath">等待遍历的目录(绝对路径)</param>
        /// <param name="result">结果</param>
        /// <returns></returns>
        public static void SearchFiles(string filePath, ref List<FileInfo> result)
        {
            DirectoryInfo folders = new DirectoryInfo(filePath);
            DirectoryInfo[] dirInfo = folders .GetDirectories();        //获取所在目录的文件夹
            FileInfo[] files = folders.GetFiles();                               //获取所在目录的文件
            
            foreach (FileInfo Item in files)                                     //遍历文件
            {
                //result += "dirName:" + Item.DirectoryName + ",fileName:" + Item.Name + "\n";        //返回路径
                result.Add(Item);
            }
           
            foreach (DirectoryInfo nextFolder in dirInfo)                 //遍历文件夹
            {
                SearchFiles(nextFolder.FullName, ref result);
            }
        }
    }
}