C# 檔案雜湊碼比較
檔案雜湊碼比較,用於更新檔案
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("兩個檔案不等");