判断两个文件是否是同一个文件

通过System.Security.Cryptography.HashAlgorithm 哈希算法获取文件的哈希值比较判断

    public static bool CompareFile(string filePath1, string filePath2)
        {
            //计算第一个文件的哈希值
            HashAlgorithm hash = HashAlgorithm.Create();
            var stream_1 = new System.IO.FileStream(filePath1, System.IO.FileMode.Open);
            byte[] hashByte_1 = hash.ComputeHash(stream_1);
            stream_1.Close();
            //计算第二个文件的哈希值
            var stream_2 = new System.IO.FileStream(filePath2, System.IO.FileMode.Open);
            byte[] hashByte_2 = hash.ComputeHash(stream_2);
            stream_2.Close();
            return BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2);
        }

 

posted @ 2018-10-12 13:47  _York  阅读(1546)  评论(5编辑  收藏  举报