Test Two Files for Equality

Problem

You need to quickly compare the content of two files.

Solution

Calculate the hash code of each file using the HashAlgorithm class, and compare the hash codes.

Discussion

There are a number of ways you might want to compare more than one file. For example, you could examine a portion of the file for similar data, or you could read through each file byte-by-byte, comparing each byte as you go. Both of these approaches are valid, but in some cases it’s more convenient to use a hash code algorithm.

A hash code algorithm generates a small (typically about 20 bytes) binary fingerprint for a file. While it’s possible for different files to generate the same hash codes, it’s statistically unlikely. In fact, even a minor change ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.