判断二个文件是否相同

判断二个文件是否相同,通过判断文件的哈希值是否一致。

        var hash = System.Security.Cryptography.HashAlgorithm.Create();
        string fileName = "Z:\Tools\Common\winx64_12102_database_1of2.zip";
        var fileStream = System.IO.File.Open(fileName, System.IO.FileMode.Open);
        var bytes1 = hash.ComputeHash(fileStream);
        fileStream.Close();

        fileName = "Z:\Tools\Common\winx64_12102_database_1of2.zip";
        fileStream = System.IO.File.Open(fileName, System.IO.FileMode.Open);
        var bytes2 = hash.ComputeHash(fileStream);
        fileStream.Close();

        if (BitConverter.ToString(bytes1) == BitConverter.ToString(bytes2))
        {
            Response.Write("相同文件");
        }
        else
        {
            Response.Write("不同文件");
        } 

  

原文地址:https://www.cnblogs.com/guohu/p/8464488.html