The hash algorithm generates a small binary "fingerprint" for a file. From a statistical perspective, different files cannot generate the same hash code.
To generate a hash code, you must first Create a HashAlgorithm object and use the HashAlgorithm. Create method. Then call
HashAlgorithm. ComputeHash method, which returns a byte array storing the hash code, and then uses BitConverter. Tostring ()
Replace it with a string for comparison.
The source code is as follows:
Copy codeThe Code is as follows: public static bool isValidFileContent (string filePath1, string filePath2)
{
// Create a hash algorithm object
Using (HashAlgorithm hash = HashAlgorithm. Create ())
{
Using (FileStream file1 = new FileStream (filePath1, FileMode. Open), file2 = new FileStream (filePath2, FileMode. Open ))
{
Byte [] hashByte1 = hash. ComputeHash (file1); // The hash algorithm obtains the byte array of the hash code based on the text.
Byte [] hashByte2 = hash. ComputeHash (file2 );
String str1 = BitConverter. ToString (hashByte1); // assemble the number of bytes into a string
String str2 = BitConverter. ToString (hashByte2 );
Return (str1 = str2); // compare the hash code
}
}
}
Main function that uses this function
Copy codeThe Code is as follows: static void Main (string [] args)
{
String filePath1 = @ "f:/1.txt ";
String filePath2 = @ "f:/2.txt ";
Bool valid = isValidFileContent (filePath1, filePath2 );
Console. WriteLine (valid. ToString ());
Console. ReadKey ();
}