C# 有没什么方法比较两文件内容不同? C# 有没什么方法比较两文件内容不同? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 两种方法,一,逐一比较两个文件的没一个字节组,直到文件结束或有不相同的地方为止 FileStream.ReadByte();二,用哈希值 //使用哈希值比较两个文件是否完全相等 System.Security.Cryptography.HashAlgorithm hash = System.Security.Cryptography.HashAlgorithm.Create(); FileStream stream1 = new FileStream("a.txt", FileMode.Open); FileStream stream2 = new FileStream("b.txt", FileMode.Open); byte[] hashbyte1 = hash.ComputeHash(stream1); byte[] hashbyte2 = hash.ComputeHash(stream2); stream1.Close(); stream2.Close(); if (BitConverter.ToString(hashbyte1) == BitConverter.ToString(hashbyte2)) MessageBox.Show("ok"); else MessageBox.Show("error"); StreamReader hc = new StreamReader(@"c:\1.txt"); string ss = hc.ReadToEnd(); StreamReader hcg = new StreamReader(@"d:\2.txt"); string sss = hcg.ReadToEnd(); if (ss == sss) { MessageBox.Show("同"); } else { MessageBox.Show("不同"); } 一应该不推荐, 如果是上G文件就完了,二还行,我试过,1G的文件算Hash也就是几秒时间.不过比较要求不是很高,通过System.IO.File的最后修改日期跟文件Size应该就差不多了吧.这就不涉及到效率问题了. C#执行存储过程超时问题 关于Visual Studio 2010新建项目的问题 能不能推荐本介绍c# ADO.NET数据库的好书 求一个学校排课系统开发文档 C# 中DataTable 对象最多可存放多少行? 这个问题…… c#在没显示器的情况下实现截图 winform下tooltip框的大小如何设置? System.ComponentModel.Win32Exception: 系统找不到指定的文件。 How to: convert stirng to a BitArray 【新手求助】C/S模式,以及传输加密 发帖的同志!请把语言组织好了再提问,说不清楚,请把主要代码发出来也可以
一,逐一比较两个文件的没一个字节组,直到文件结束或有不相同的地方为止
FileStream.ReadByte();
二,用哈希值
System.Security.Cryptography.HashAlgorithm hash = System.Security.Cryptography.HashAlgorithm.Create();
FileStream stream1 = new FileStream("a.txt", FileMode.Open);
FileStream stream2 = new FileStream("b.txt", FileMode.Open);
byte[] hashbyte1 = hash.ComputeHash(stream1);
byte[] hashbyte2 = hash.ComputeHash(stream2);
stream1.Close();
stream2.Close(); if (BitConverter.ToString(hashbyte1) == BitConverter.ToString(hashbyte2))
MessageBox.Show("ok");
else
MessageBox.Show("error");
string ss = hc.ReadToEnd();
StreamReader hcg = new StreamReader(@"d:\2.txt");
string sss = hcg.ReadToEnd();
if (ss == sss)
{
MessageBox.Show("同");
}
else
{
MessageBox.Show("不同");
}
一应该不推荐, 如果是上G文件就完了,
二还行,我试过,1G的文件算Hash也就是几秒时间.
不过比较要求不是很高,通过System.IO.File的最后修改日期跟文件Size应该就差不多了吧.
这就不涉及到效率问题了.