欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-09-24 10:35:25
通过System.Security.Cryptography.HashAlgorithm 哈希算法获取文件的哈希值比较判断 ......

通过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);
        }