A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-22 17:58  /  1124 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

将自定义比较器类与标准查询运算符一起使用。
  1. using System.IO;
  2. using System.Collections.Generic;
  3. namespace QueryCompareTwoDirs
  4. {
  5.     class CompareDirs
  6.     {

  7.         static void Main(string[] args)
  8.         {

  9.             // Create two identical or different temporary folders
  10.             // on a local drive and change these file paths.
  11.             string pathA = @"C:\TestDir";
  12.             string pathB = @"C:\TestDir2";

  13.             DirectoryInfo dir1 = new DirectoryInfo(pathA);
  14.             DirectoryInfo dir2 = new DirectoryInfo(pathB);

  15.             // Take a snapshot of the file system.
  16.             IEnumerable<FileInfo> list1 = dir1.GetFiles("*.*", SearchOption.AllDirectories);
  17.             IEnumerable<FileInfo> list2 = dir2.GetFiles("*.*", SearchOption.AllDirectories);

  18.             //A custom file comparer defined below
  19.             FileCompare myFileCompare = new FileCompare();

  20.             // This query determines whether the two folders contain
  21.             // identical file lists, based on the custom file comparer
  22.             // that is defined in the FileCompare class.
  23.             // The query executes immediately because it returns a bool.
  24.             bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

  25.             if (areIdentical == true)
  26.             {
  27.                 Console.WriteLine("the two folders are the same");
  28.             }
  29.             else
  30.             {
  31.                 Console.WriteLine("The two folders are not the same");
  32.             }

  33.             // Find the common files. It produces a sequence and doesn't
  34.             // execute until the foreach statement.
  35.             var queryCommonFiles = list1.Intersect(list2, myFileCompare);

  36.             if (queryCommonFiles.Count() > 0)
  37.             {
  38.                 Console.WriteLine("The following files are in both folders:");
  39.                 foreach (var v in queryCommonFiles)
  40.                 {
  41.                     Console.WriteLine(v.FullName); //shows which items end up in result list
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 Console.WriteLine("There are no common files in the two folders.");
  47.             }

  48.             // Find the set difference between the two folders.
  49.             // For this example we only check one way.
  50.             var queryList1Only = (from file in list1
  51.                                   select file).Except(list2, myFileCompare);

  52.             Console.WriteLine("The following files are in list1 but not list2:");
  53.             foreach (var v in queryList1Only)
  54.             {
  55.                 Console.WriteLine(v.FullName);
  56.             }

  57.             // Keep the console window open in debug mode.
  58.             Console.WriteLine("Press any key to exit.");
  59.             Console.ReadKey();
  60.         }
  61.     }

  62.     // This implementation defines a very simple comparison
  63.     // between two FileInfo objects. It only compares the name
  64.     // of the files being compared and their length in bytes.
  65.     class FileCompare : IEqualityComparer<FileInfo>
  66.     {
  67.         public FileCompare() { }

  68.         public bool Equals(FileInfo f1, FileInfo f2)
  69.         {
  70.             return (f1.Name == f2.Name &&
  71.                     f1.Length == f2.Length);
  72.         }

  73.         // Return a hash that reflects the comparison criteria. According to the
  74.         // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
  75.         // also be equal. Because equality as defined here is a simple value equality, not
  76.         // reference identity, it is possible that two or more objects will produce the same
  77.         // hash code.
  78.         public int GetHashCode(FileInfo fi)
  79.         {
  80.             string s = String.Format("{0}{1}", fi.Name, fi.Length);
  81.             return s.GetHashCode();
  82.         }
  83.     }
  84. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马