Home > Backend Development > C#.Net Tutorial > How to determine whether two files are the same file (code)

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

云罗郡主
Release: 2018-10-15 14:52:09
forward
4609 people have browsed it

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);
        }
Copy after login

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!

source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template