比较两个文件夹的内容

使用LINQ比较两个文件夹内容(长度和文件名)

 

 1  static  void CompareTwoFolders()
 2         {
 3              //  Create two identical or different temporary folders 
 4               //  on a local drive and change these file paths.
 5               string pathA =  @" C:\TestDir ";
 6              string pathB =  @" C:\TestDir2 ";
 7 
 8             System.IO.DirectoryInfo dir1 =  new System.IO.DirectoryInfo(pathA);
 9             System.IO.DirectoryInfo dir2 =  new System.IO.DirectoryInfo(pathB);
10 
11              //  Take a snapshot of the file system.
12              IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles( " *.* ", System.IO.SearchOption.AllDirectories);
13             IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles( " *.* ", System.IO.SearchOption.AllDirectories);
14 
15              // A custom file comparer defined below
16              FileCompare myFileCompare =  new FileCompare();
17 
18              bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
19 
20              if (areIdentical ==  true)
21             {
22                 Console.WriteLine( " the two folders are the same ");
23             }
24              else
25             {
26                 Console.WriteLine( " The two folders are not the same ");
27             }
28 
29              //  Find the common files. It produces a sequence and doesn't 
30               //  execute until the foreach statement.
31               var queryCommonFiles = list1.Intersect(list2, myFileCompare);
32 
33              if (queryCommonFiles.Count() >  0)
34             {
35                 Console.WriteLine( " The following files are in both folders: ");
36                  foreach ( var v  in queryCommonFiles)
37                 {
38                     Console.WriteLine(v.FullName);  // shows which items end up in result list
39                  }
40             }
41              else
42             {
43                 Console.WriteLine( " There are no common files in the two folders. ");
44             }
45 
46              //  Find the set difference between the two folders.
47               //  For this example we only check one way.
48               var queryList1Only = ( from file  in list1
49                                    select file).Except(list2, myFileCompare);
50 
51             Console.WriteLine( " The following files are in list1 but not list2: ");
52              foreach ( var v  in queryList1Only)
53             {
54                 Console.WriteLine(v.FullName);
55             }
56 
57         }
58 
59          class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
60         {
61              public FileCompare() { }
62 
63              public  bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
64             {
65                  return (f1.FullName == f2.FullName &&
66                         f1.Length == f2.Length);
67             }
68 
69              public  int GetHashCode(System.IO.FileInfo fi)
70             {
71                  string s = String.Format( " {0}{1} ", fi.Name, fi.Length);
72                  return s.GetHashCode();
73             }
74         }

 

转载于:https://www.cnblogs.com/dingshouqing/archive/2012/03/12/2392228.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值