---
title: "How can you compare two files byte by byte to check if they are identical?"  
description: "How can you compare two files byte by byte to check if they are identical?"  
author: "ICSM Computer"  
published: 2025-05-14  
updated: 2025-05-28  
canonical: https://www.mindstick.com/forum/161621/how-can-you-compare-two-files-byte-by-byte-to-check-if-they-are-identical  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How can you compare two files byte by byte to check if they are identical?

How can you [compare two](https://www.mindstick.com/forum/34476/specific-cast-is-not-valid-in-linq-query-when-compare-two-tables) [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) byte by byte to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) they are identical?

## Replies

### Reply by Utpal Vishwas

To [compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes) [two files](https://www.mindstick.com/interview/2597/can-you-have-two-files-with-the-same-file-name-in-gac) byte by byte in C#, you can use `FileStream` or `File.ReadAllBytes` for a full comparison. A more memory-efficient way is to stream the files and compare them block by block.

### Example: Efficient Byte-by-Byte Comparison Using Streams

```cs
public static bool AreFilesIdentical(string filePath1, string filePath2)
{
    const int bufferSize = 1024 * 1024; // 1 MB buffer

    var fileInfo1 = new FileInfo(filePath1);
    var fileInfo2 = new FileInfo(filePath2);

    if (fileInfo1.Length != fileInfo2.Length)
        return false;

    using (var fs1 = new FileStream(filePath1, FileMode.Open, FileAccess.Read))
    using (var fs2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read))
    {
        var buffer1 = new byte[bufferSize];
        var buffer2 = new byte[bufferSize];

        int bytesRead1, bytesRead2;
        do
        {
            bytesRead1 = fs1.Read(buffer1, 0, bufferSize);
            bytesRead2 = fs2.Read(buffer2, 0, bufferSize);

            if (bytesRead1 != bytesRead2)
                return false;

            for (int i = 0; i < bytesRead1; i++)
            {
                if (buffer1[i] != buffer2[i])
                    return false;
            }

        } while (bytesRead1 > 0);
    }

    return true;
}
```

### Notes

1. This approach avoids loading entire files into memory, making it suitable for large files.
2. Comparing length first is a quick way to eliminate obviously unequal files.
3. A `File.ReadAllBytes` approach is simpler but less efficient for large files.


---

Original Source: https://www.mindstick.com/forum/161621/how-can-you-compare-two-files-byte-by-byte-to-check-if-they-are-identical

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
