判斷兩個文件是否是同一個文件


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM