5.12. Test Two Files for Equality

Problem

You need to quickly compare the content of two files and determine whether it matches exactly.

Solution

Calculate the hash code of each file using the System.Security.Cryptography.HashAlgorithm class, and then compare the hash codes.

How It Works

You might compare file content in a number of ways. 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 ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach 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.