I am trying to compare two directories to see which files are in directory 1, which are not in directory 2. I have the following code:
System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA); System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB); IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.PRN"); IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.PRN"); IEnumerable<System.IO.FileInfo> list3 = list1.Except(list2); Console.WriteLine("The following files are in list1 but not list2:"); foreach (var v in list3) { Console.WriteLine(v); } Console.WriteLine("Press any key to continue..."); Console.ReadKey();
When this is done, it clearly lists all the files that are in directory 1, but many of them are already in directory 2. I can see this simply by looking at Windows Explorer and looking at the file names. What am I missing?
EDIT:
I believe the problem is in the file comparison section. I am trying to make it ignore the case of a file extension. I tried this:
class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo> { public FileCompare() { } public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2) {
But that still doesn't work. You can see that I commented on another attempt to simply make all uppercase in comparison, but that is not the case.
source share