1:递归获取指定目录下的指定类型的文件 public class FileTool { /// <summary> /// 私有变量 /// </summary> private static List<FileInfo> lst = new List<FileInfo>(); /// <summary> /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹) /// </summary> /// <param name="path">文件夹路径</param> /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param> /// <returns>List<FileInfo></returns> public static List<FileInfo> GetFile(string path, string extName) { GetDir(path, extName); return lst; } /// <summary> /// 递归获取指定类型文件,包含子文件夹 /// </summary> /// <param name="path"></param> /// <param name="extName"></param> private static void GetDir(string path, string extName) { try { if (!Directory.Exists(path)) { return; } string[] dir = Directory.GetDirectories(path); //文件夹列表 DirectoryInfo fdir = new DirectoryInfo(path); FileInfo[] file = fdir.GetFiles(); if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空 { foreach (FileInfo f in file) //显示当前目录所有文件 { if (extName.ToLower().IndexOf(f.Extension.ToLower() , StringComparison.Ordinal) >= 0) { lst.Add(f); } } foreach (string d in dir) { GetDir(d, extName);//递归 } } } catch (Exception ex) { Flag.LogTool.LogTool.WriteLog(ex.StackTrace); } } }
标签:文件,string,递归,指定,extName,文件夹,path,目录 From: https://www.cnblogs.com/leon1128/p/17822913.html