Skip to main content

This C# helper method to compare two files, byte-by-byte.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

//
// This method accepts two strings the represent two files to
// compare. A return value of true indicates that the contents of the files
// are the same. A return value of any other value indicates that the
// files are not the same.
public static bool FileCompare(string filePath1, string filePath2)
{
    int file1byte;
    int file2byte;

    // Determine if the same file was referenced two times.
    if (String.Equals(filePath1, filePath2, StringComparison.CurrentCultureIgnoreCase))
    {
        // Return true to indicate that the files are the same.
        return true;
    }

    // Open the two files.
    using(FileStream fs1 = new FileStream(filePath1, FileMode.Open, FileAccess.Read))
    {
        using(FileStream fs2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read))
        {
            // Check the file sizes. If they are not the same, the files
            // are not the same.
            if (fs1.Length != fs2.Length)
            {
                // Return false to indicate files are different
                return false;
            }

            // Read and compare a byte from each file until either a
            // non-matching set of bytes is found or until the end of
            // file1 is reached.
            do
            {
                // Read one byte from each file.
                file1byte = fs1.ReadByte();
                file2byte = fs2.ReadByte();
            }
            while ((file1byte == file2byte) && (file1byte != -1));
        }
    }

    // Return the success of the comparison. "file1byte" is
    // equal to "file2byte" at this point only if the files are
    // the same.
    return ((file1byte - file2byte) == 0);
}