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.
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.
Post A Reply