打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
LINQ和文件目录

记录https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/linq/linq-and-file-directories的学习

 

查询具有指定扩展名的文件 (SearchOption.AllDirectories 指递归文件夹获取所有文件)

string startFolder = @"C:\Users\bibi\Desktop\代码\异步\ConsoleApp4\Test\";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:\Users\bibi\Desktop\代码\异步\ConsoleApp4\Test\";// 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:\Users\bibi\Desktop\代码\异步\ConsoleApp4\Test\";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:\Users\bibi\Desktop\代码\异步\ConsoleApp4\Test\";string pathB = @"C:\Users\bibi\Desktop\代码\异步\ConsoleApp4\Test\TestX\";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);}

 查找目录中最大、最小的一个或多个文件。

using System;using System.Collections.Generic;using System.Data;using System.Diagnostics.CodeAnalysis;using System.IO;using System.Linq;using System.Reflection;namespace ConsoleApp4{    class Program    {        static void Main(string[] args)        {            string startFolder = @"C:\Users\mycomputer\Desktop\代码\异步\ConsoleApp4\Test\TestX\";            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);                       IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);            //查找文件最大字节数            long maxSize = fileList.Select(x => GetFileLength(x)).Max();                       Console.WriteLine($"The length of the largest file under {startFolder} is {maxSize}");            //查找字节数最大的文件            var longestFile = fileList.OrderByDescending(x => GetFileLength(x)).First();                      Console.WriteLine("The largest file under {0} is {1} with a length of {2} bytes",                                startFolder, longestFile.FullName, longestFile.Length);            //查找字节数最小的文件            var smallestFile = fileList.OrderBy(x => GetFileLength(x)).First();                        Console.WriteLine("The smallest file under {0} is {1} with a length of {2} bytes",                                startFolder, smallestFile.FullName, smallestFile.Length);           //查找字节前十大的文件            var queryTenLargest = fileList.OrderByDescending(x => GetFileLength(x)).Take(10);                    Console.WriteLine("The 10 largest files under {0} are:", startFolder);            foreach (var v in queryTenLargest)            {                Console.WriteLine("{0}: {1} bytes", v.FullName, v.Length);            }        }        static long GetFileLength(System.IO.FileInfo fi)        {            long bytes;            try            {                bytes = fi.Length;            }            catch (System.IO.FileNotFoundException)            {                //如果文件找不到 0字节处理                bytes = 0;            }            return bytes;        }    }   }
View Code

查询目录树中重复文件。

using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Linq;namespace ConsoleApp4{    class Program    {        static void Main(string[] args)        {            string startFolder = @"C:\Users\biubiu\Desktop\代码\异步\ConsoleApp4\Test\";            DirectoryInfo dir = new DirectoryInfo(startFolder);                       IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);            //根据文件名字、最后更新时间、字节长度来判断文件重复            var querySameGroup = from file in fileList                                     group file.Name by new PortableKey { Name = file.Name, LastWriteTime = file.LastWriteTime, Length = file.Length } into fileGroup                                     where fileGroup.Count() > 1                                     select fileGroup;            //Linq 方法调用写法             //var querySameGroup2 = fileList.GroupBy(file => new PortableKey            //{            //    LastWriteTime = file.LastWriteTime,            //    Length = file.Length,            //    Name = file.Name            //}, file => file.Name).Where(fileGroup => fileGroup.Count() > 1);            foreach(var group in querySameGroup)            {                Console.WriteLine(group.Key);                foreach(var item in group)                {                    Console.WriteLine($"  {item}");                }            }                }           }    class PortableKey    {        public string Name { get; set; }        public DateTime LastWriteTime { get; set; }        public long Length { get; set; }        //分组跟这个相关        public override bool Equals(object obj)        {            PortableKey other = (PortableKey)obj;            return this.LastWriteTime == other.LastWriteTime &&                this.Length == other.Length &&                this.Name == other.Name;        }        public override int GetHashCode()        {            string str = $"{this.LastWriteTime}{this.Length}{this.Name}";            return str.GetHashCode();        }        public override string ToString()        {            return $"{this.Name} {this.Length} {this.LastWriteTime}";        }    }}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#获取文件夹下的所有文件的文件名
浅析C#中的文件操作
C#中如何创建文件夹
C#操作目录和文件
浅析文本文件的基本操作
C#各种文件操作的代码与注释
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服