C# 文件哈希码比较
文件哈希碼比較,用于更新文件
public static bool CompareFile(string str1, string str2)
? ? ? ? {
? ? ? ? ? ? string p_1 = str1;
? ? ? ? ? ? string p_2 = str2;
? ? ? ? ? ? //計(jì)算第一個(gè)文件的哈希值
? ? ? ? ? ? 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();
? ? ? ? ? ? //計(jì)算第二個(gè)文件的哈希值
? ? ? ? ? ? var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
? ? ? ? ? ? byte[] hashByte_2 = hash.ComputeHash(stream_2);
? ? ? ? ? ? stream_2.Close();
? ? ? ? ? ? //比較兩個(gè)哈希值
? ? ? ? ? ? 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("兩個(gè)文件相等");
else
? ? MessageBox.Show("兩個(gè)文件不等");
總結(jié)
以上是生活随笔為你收集整理的C# 文件哈希码比较的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: win10更新后,wsappx占用高内存
- 下一篇: 移动开发作业:三种近场通信技术的特点及对