Compare two files in C#
This post will discuss how to compare two files in C#.
1. Using FileStream.ReadByte() method
The idea here is to compare the given files byte-by-byte. The FileStream.ReadByte() method reads a byte from the file and advances the read position by one byte. This might be slow, but only a feasible performance-optimized solution for large files.
The following solution demonstrates the byte by byte comparison between two files. It reads data from a file, byte by byte, compares each byte for equality, and proceeds to the next byte.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; using System.IO; public class Program { private static bool hasSameContent(string path1, string path2) { var f1 = new FileInfo(path1); var f2 = new FileInfo(path2); if (f1.Length != f2.Length) { return false; } if (string.Equals(f1.FullName, f2.FullName, StringComparison.OrdinalIgnoreCase)) { return true; } using (FileStream fs1 = f1.OpenRead()) using (FileStream fs2 = f2.OpenRead()) { for (int i = 0; i < f1.Length; i++) { if (fs1.ReadByte() != fs2.ReadByte()) { return false; } } } return true; } public static void Main() { string path1 = @"C:\first.txt"; string path2 = @"C:\second.txt"; bool isSame = hasSameContent(path1, path2); if (isSame) { Console.WriteLine("Both files have same content"); } else { Console.WriteLine("Both files have different content"); } } } |
2. Using File.ReadAllBytes() method
When you are working with small files, you can read the entire contents of the file into a byte array using the File.ReadAllBytes() method, and check both byte arrays for equality using the Enumerable.SequenceEqual() method. This is the easiest solution for small files:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.IO; public class Program { public static void Main() { string path1 = @"C:\first.txt"; string path2 = @"C:\second.txt"; bool isSame = File.ReadAllBytes(path1).SequenceEqual(File.ReadAllBytes(path2)); if (isSame) { Console.WriteLine("Both files have same content"); } else { Console.WriteLine("Both files have different content"); } } } |
3. Using File.ReadLines() method
Alternatively, you can use the File.ReadLines() method to read all the lines of the file into an IEnumerable<String>. This method can accept an encoding to use to read the file. Since it returns an enumerable, the whole file is not returned. The File.ReadAllBytes() method, on the other hand, returns the whole file as an array of strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.IO; public class Program { public static void Main() { string path1 = @"C:\first.txt"; string path2 = @"C:\second.txt"; bool isSame = File.ReadLines(path1).SequenceEqual(File.ReadLines(path2)); if (isSame) { Console.WriteLine("Both files have same content"); } else { Console.WriteLine("Both files have different content"); } } } |
That’s all about comparing two files in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)