代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
比较两个文件夹的内容 (LINQ)
作者:
DDT
/ 发布于
2012/10/8
/
657
将自定义比较器类与标准查询运算符一起使用。
<div>using System.IO; using System.Collections.Generic; namespace QueryCompareTwoDirs { class CompareDirs {</div> <div> static void Main(string[] args) {</div> <div> // Create two identical or different temporary folders // on a local drive and change these file paths. string pathA = @"C:\TestDir"; string pathB = @"C:\TestDir2";</div> <div> DirectoryInfo dir1 = new DirectoryInfo(pathA); DirectoryInfo dir2 = new DirectoryInfo(pathB);</div> <div> // Take a snapshot of the file system. IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories); IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);</div> <div> //A custom file comparer defined below FileCompare myFileCompare = new FileCompare();</div> <div> // This query determines whether the two folders contain // identical file lists, based on the custom file comparer // that is defined in the FileCompare class. // The query executes immediately because it returns a bool. bool areIdentical = list1.SequenceEqual(list2, myFileCompare);</div> <div> if (areIdentical == true) { Console.WriteLine("the two folders are the same"); } else { Console.WriteLine("The two folders are not the same"); } <div> // Find the common files. It produces a sequence and doesn't // execute until the foreach statement. var queryCommonFiles = list1.Intersect(list2, myFileCompare);</div> <div> if (queryCommonFiles.Count() > 0) { 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 } } else { Console.WriteLine("There are no common files in the two folders."); } <div> // Find the set difference between the two folders. // For this example we only check one way. var queryList1Only = (from file in list1 select file).Except(list2, myFileCompare);</div> <div> Console.WriteLine("The following files are in list1 but not list2:"); foreach (var v in queryList1Only) { Console.WriteLine(v.FullName); } <div> // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } <div> // This implementation defines a very simple comparison // between two FileInfo objects. It only compares the name // of the files being compared and their length in bytes. class FileCompare : IEqualityComparer<FileInfo> { public FileCompare() { } <div> public bool Equals(FileInfo f1, FileInfo f2) { return (f1.Name == f2.Name && f1.Length == f2.Length); } <div> // Return a hash that reflects the comparison criteria. According to the // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must // also be equal. Because equality as defined here is a simple value equality, not // reference identity, it is possible that two or more objects will produce the same // hash code. public int GetHashCode(FileInfo fi) { string s = String.Format("{0}{1}", fi.Name, fi.Length); return s.GetHashCode(); } } } </div>
试试其它关键字
比较两个文件夹
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3