Comparing files

starstarstarstarstarstarstarstarstarstar Rating: 0/5 (0 vote cast) print
One way to compare files is to use hashing. A hash value (also called a
message digest) is a number generated from a text. The hash is
substantially smaller than the text itself, and is generated by a
formula in such a way that it is extremely unlikely that some other
text will produce the same hash value.

Please have a look to the following code snippet to see implementation of files comparing with hash mechanism.

using System.IO;

using System.Security.Cryptography; 


private bool AreFilesIdentical(string source, string target)


            //create the hashing object.

            HashAlgorithm alg = HashAlgorithm.Create();

            //Calculate the hash for the first file

            FileStream fsA = new FileStream(source, FileMode.Open);

            byte[] hashA = alg.ComputeHash(fsA);



            //calculate the hash for the second file

            FileStream fsB = new FileStream(target, FileMode.Open);

            byte[] hashB = alg.ComputeHash(fsB);



            //compare the hashes

return BitConverter.ToString(hashA) == BitConverter.ToString(hashB);


Another solution would be to use file metadata information. But you
can’t count on it every time. E.g. if you based on file time
modification stamp, you might have surprises in case of file originate
on system with different time zones.

I’m waiting your suggestions.

 : Mihai Bejenariu     Reply  

Replies (1)

Another way of comparing files is using special software. I use the program called Compare Suite (

This software allows comparing files of different formats (*.txt, *.doc, *xls *.html, *.pdf etc.) + has folder comparison feature and also has reporting function.

I have been using it for quite long and had no complaints. The free download is available on their website.

: JasonD    Reply

Post A Reply

 Questions & Answers