Comparing files using MD5

Following C# function takes the two files path as arguments and return true if these two file are
same or false in other case.

public bool CampareFiles(string strFilePath1, string strFilePath2)
{
bool bReturn = false;
System.Security.Cryptography.MD5 oMD5 = MD5.Create();
byte[] checksum = null;
string strchecksum1 = ” “;
string strchecksum2 = ” “;

try
{
//Getting Checksum for file 1 in string format
using (FileStream stream = File.OpenRead(strFilePath1))
{
checksum = oMD5.ComputeHash(stream);
//Converting in string format
strchecksum1 = BitConverter.ToString(checksum).Replace(“-“, String.Empty);
}

using (FileStream stream = File.OpenRead(strFilePath2))
{
checksum = oMD5.ComputeHash(stream);
strchecksum2 = BitConverter.ToString(checksum).Replace(“-“, String.Empty);
}

//Comparing Checksum
if (strchecksum1 == strchecksum2)
{
bReturn = false;
}
return bReturn;
}
catch (Exception ex)
{
//Catch Error here if any
}
return bReturn;
}

Leave a comment