C# 遞歸查找文件夾下所有文件和子文件夾的所有文件
阿新 • • 發佈:2017-10-25
pub list c# json tor -- where 所有 ont
- 方法實現
- public class DirectoryAllFiles
- {
- static List<FileInformation> FileList = new List<FileInformation>();
- public static List<FileInformation> GetAllFiles(DirectoryInfo dir)
- {
- FileInfo[] allFile = dir.GetFiles();
- foreach (FileInfo fi in allFile)
- {
- FileList.Add(new FileInformation{ FileName=fi.Name,FilePath=fi.FullName });
- }
- DirectoryInfo[] allDir= dir.GetDirectories();
- foreach (DirectoryInfo d in allDir)
- {
- GetAllFiles(d);
- }
- return FileList;
- }
- }
- public class FileInformation
- {
- public string FileName { get; set; }
- public string FilePath { get; set; }
- }
- 方法調用: List<FileInformation> list = DirectoryAllFiles.GetAllFiles(
new System.IO.DirectoryInfo(@"E:\Test")); if (list.Where(t => t.FileName.ToLower().Contains("json")).Any()) Console.WriteLine("有信息"); foreach (var item in list)
- {
- Console.WriteLine(
string.Format("文件名:{0}---文件目錄{1}",item.FileName,item.FilePath));
- }
C# 遞歸查找文件夾下所有文件和子文件夾的所有文件