Home  >  Article  >  Backend Development  >  How to determine whether two files are the same file (code)

How to determine whether two files are the same file (code)

云罗郡主
云罗郡主forward
2018-10-15 14:52:094578browse

The content of this article is about how to determine whether two files are the same file (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Obtain the hash value comparison and judgment of the file through the System.Security.Cryptography.HashAlgorithm hash algorithm

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

The above is how to judge the two Whether each file is the same file (code) is all introduced. If you want to know more about C video tutorial, please pay attention to the PHP Chinese website.

The above is the detailed content of How to determine whether two files are the same file (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete