/// <summary> /// 返回文件夹及子目录的文件夹 /// </summary> /// <param name="directory"></param> /// <param name="files"></param> public static void GetFiles(string directory, ref Dictionary<string, FileInfo> files) { DirectoryInfo di = new DirectoryInfo(directory); if (di.Exists) { foreach (FileInfo f in di.GetFiles()) { if (files.ContainsKey(f.FullName)) continue; files.Add(f.FullName, f); } foreach (DirectoryInfo d in di.GetDirectories()) { GetFiles(d.FullName, ref files); } } } /// <summary> /// 返回指定类型的文 /// </summary> /// <param name="directory"></param> /// <param name="extension"></param> /// <param name="files"></param> public static void GetFiles(string directory, string extension, ref Dictionary<string, FileInfo> files) { DirectoryInfo di = new DirectoryInfo(directory); if (di.Exists) { foreach (FileInfo f in di.GetFiles($"*{extension}")) { if (files.ContainsKey(f.FullName)) continue; files.Add(f.FullName, f); } foreach (DirectoryInfo d in di.GetDirectories()) { GetFiles(d.FullName, extension, ref files); } } }
//调用方式
Dictionary<string,FileInfo> files = new Dictionary<string,FileInfo>(); GetFiles(AppContext.BaseDirectory,".jpg", ref files);
标签:files,GetFiles,di,C#,子目录,DirectoryInfo,文件夹,FullName,ref From: https://www.cnblogs.com/rovedog/p/18302541