LINQ和文件目录
Posted bibi-feiniaoyuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LINQ和文件目录相关的知识,希望对你有一定的参考价值。
查询具有指定扩展名的文件 (SearchOption.AllDirectories 指递归文件夹获取所有文件)
string startFolder = @"C:UsersibiDesktop代码异步ConsoleApp4Test"; DirectoryInfo dir = new DirectoryInfo(startFolder); IEnumerable<FileInfo> fileList = dir.GetFiles("*.*", SearchOption.AllDirectories); var fileQuery = from file in fileList where file.Extension == ".png" orderby file.Name select file; foreach(var item in fileQuery) { Console.WriteLine(item.FullName); }
按扩展名对文件进行分组
// Take a snapshot of the file system. string startFolder = @"C:UsersibiDesktop代码异步ConsoleApp4Test"; // Used in WriteLine to trim output lines. int trimLength = startFolder.Length; // Take a snapshot of the file system. System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder); // This method assumes that the application has discovery permissions // for all folders under the specified path. IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories); var queryGroupByExt = from file in fileList group file by file.Extension.ToLower() into fileGroup orderby fileGroup.Key select fileGroup; foreach(var group in queryGroupByExt) { Console.WriteLine(group.Key); foreach(var item in group) { Console.WriteLine($" {item.Name}"); } }
求目录下的所有文件的总字节数
string startFolder = @"C:UsersibiDesktop代码异步ConsoleApp4Test"; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder); var totalLength = dir.GetFiles().Sum(x=>x.Length); Console.WriteLine(totalLength+" bytes");
对接文件夹中的文件(序列求等SequenceEqual、交集Insect、差集Except)
string pathA = @"C:UsersibiDesktop代码异步ConsoleApp4Test"; string pathB = @"C:UsersibiDesktop代码异步ConsoleApp4TestTestX"; System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA); System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
//使用了顶层目录SearchOption.TopDirectoryOnly,不需递归目录下的文件夹寻找文件,只要第一层文件。 IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.TopDirectoryOnly); IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.TopDirectoryOnly); //使用自定义的文件默认比较器 FileCompare myFileCompare = new FileCompare(); //判断两个序列是否相等 bool isEqualOne = list1.SequenceEqual(list2, myFileCompare); if (isEqualOne == true) { Console.WriteLine("the two folders are the same"); } else { Console.WriteLine("The two folders are not the same"); } //求交集文件 var queryCommonFiles = list1.Intersect(list2, myFileCompare); if (queryCommonFiles.Any()) { Console.WriteLine("The following files are in both folders:"); foreach (var v in queryCommonFiles) { Console.WriteLine(v.FullName); //shows which items end up in result list } } //求差集文件 在list1却不在list2的文件 var queryList1Only = list1.Except(list2, myFileCompare); Console.WriteLine("The following files are in list1 but not list2:"); foreach (var v in queryList1Only) { Console.WriteLine(v.FullName); }
以上是关于LINQ和文件目录的主要内容,如果未能解决你的问题,请参考以下文章
错误 3002:映射片段中的问题 | c# linq 到实体
在 C# 中列出文件夹内的重复文件:利用 LINQ.AsParallel
Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段
Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段