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