public static bool isValidFileContent(string filePath1, string filePath2)
{
//创建一个哈希算法对象
using (HashAlgorithm hash = HashAlgorithm.Create())
{
using (FileStream file1 = new FileStream(filePath1, FileMode.Open),file2=new FileStream(filePath2,FileMode.Open))
{
byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组
byte[] hashByte2 = hash.ComputeHash(file2);
string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
string str2 = BitConverter.ToString(hashByte2);
return (str1==str2);//比较哈希码
}
}
}
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();
}