C#比較兩個文本文件的內容是否相等


1.引用:using System.IO;

2.代碼:

class Program
{
static void Main(string[] args)
{
string filePath1 = "F:\\測試文件\\11.txt";
string filePath2 = "F:\\測試文件\\22.txt";
using (HashAlgorithm hash = HashAlgorithm.Create())
{
using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
{
byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根據文本得到哈希碼的字節數組
byte[] hashByte2 = hash.ComputeHash(file2);
string str1 = BitConverter.ToString(hashByte1);//將字節數組裝換為字符串
string str2 = BitConverter.ToString(hashByte2);
if (str1 == str2)
{

Console.WriteLine("相同");

}
else
{

Console.WriteLine("不同");

}
}
Console.ReadKey();
}
}

}

 


免責聲明!

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



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