文件哈希码比较,用于更新文件
public static bool CompareFile(string str1, string str2)
{
string p_1 = str1;
string p_2 = str2;
//计算第一个文件的哈希值
var hash = System.Security.Cryptography.HashAlgorithm.Create();
var stream_1 = new System.IO.FileStream(p_1, System.IO.FileMode.Open);
byte[] hashByte_1 = hash.ComputeHash(stream_1);
stream_1.Close();
//计算第二个文件的哈希值
var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
byte[] hashByte_2 = hash.ComputeHash(stream_2);
stream_2.Close();
//比较两个哈希值
if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
{
return true;
}
else
{
return false;
}
}
主程序
bool b = CompareFile("F:\\CDllTest1.dll", "F:\\CDllTest2.dll");
if (b)
MessageBox.Show("两个文件相等");
else
MessageBox.Show("两个文件不等");