使用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 }
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 }