用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

比较两个文件夹中的内容

2012-10-19 作者: 程序猿style举报

[c#]代码库

using System.IO;
using System.Collections.Generic;
namespace QueryCompareTwoDirs
{
class CompareDirs
{
	static void Main ( string[] args )
	{
		// Create two identical or different temporary folders
		// on a local drive and change these file paths.
		string pathA = @"C:\TestDir";
		string pathB = @"C:\TestDir2";
		DirectoryInfo dir1 = new DirectoryInfo ( pathA );
		DirectoryInfo dir2 = new DirectoryInfo ( pathB );
		// Take a snapshot of the file system.
		IEnumerable list1 = dir1.GetFiles ( "*.*", SearchOption.AllDirectories );
		IEnumerable list2 = dir2.GetFiles ( "*.*", SearchOption.AllDirectories );
		//A custom file comparer defined below
		FileCompare myFileCompare = new FileCompare();
		// 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 );
		if ( areIdentical == true )
		{
			Console.WriteLine ( "the two folders are the same" );
		}
		else
		{
			Console.WriteLine ( "The two folders are not the same" );
		}
		// Find the common files. It produces a sequence and doesn't
		// execute until the foreach statement.
		var queryCommonFiles = list1.Intersect ( list2, myFileCompare );
		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." );
		}
		// 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 );
		Console.WriteLine ( "The following files are in list1 but not list2:" );
		foreach ( var v in queryList1Only )
		{
			Console.WriteLine ( v.FullName );
		}
		// Keep the console window open in debug mode.
		Console.WriteLine ( "Press any key to exit." );
		Console.ReadKey();
	}
}
// 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
{
	public FileCompare() { }
	public bool Equals ( FileInfo f1, FileInfo f2 )
	{
		return ( f1.Name == f2.Name &&
		         f1.Length == f2.Length );
	}
	// Return a hash that reflects the comparison criteria. According to the
	// rules for IEqualityComparer, 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();
	}
}
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...