欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

判断两个文件是否相同

程序员文章站 2022-05-16 09:34:00
using (HashAlgorithm hash = HashAlgorithm.Create()) { using (FileStream file1 = new FileStream(FilePath1, FileMode.Open), file2 = new FileStream(FileP ......

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)
{

//write

}
else
{

//write

}
}
}