我想在C#中比较两个文件并查看它们是否不同。 它们具有相同的文件名,并且当它们不同时它们的大小完全相同。 我只是想知道是否有快速的方法可以在不手动读取文件的情况下完成此操作。
谢谢
我想在C#中比较两个文件并查看它们是否不同。 它们具有相同的文件名,并且当它们不同时它们的大小完全相同。 我只是想知道是否有快速的方法可以在不手动读取文件的情况下完成此操作。
谢谢
根据你想达到的目的,你可以看一下 Diff.NET
这里是一个简单的文件比较函数:
// This method accepts two strings the represent two files to
// compare. A return value of 0 indicates that the contents of the files
// are the same. A return value of any other value indicates that the
// files are not the same.
private bool FileCompare(string file1, string file2)
{
int file1byte;
int file2byte;
FileStream fs1;
FileStream fs2;
// Determine if the same file was referenced two times.
if (file1 == file2)
{
// Return true to indicate that the files are the same.
return true;
}
// Open the two files.
fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read);
fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read);
// Check the file sizes. If they are not the same, the files
// are not the same.
if (fs1.Length != fs2.Length)
{
// Close the file
fs1.Close();
fs2.Close();
// Return false to indicate files are different
return false;
}
// Read and compare a byte from each file until either a
// non-matching set of bytes is found or until the end of
// file1 is reached.
do
{
// Read one byte from each file.
file1byte = fs1.ReadByte();
file2byte = fs2.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
// Close the files.
fs1.Close();
fs2.Close();
// Return the success of the comparison. "file1byte" is
// equal to "file2byte" at this point only if the files are
// the same.
return ((file1byte - file2byte) == 0);
}
我只是在想是否有一种快速的方法可以不必手动读取文件就能完成这项任务。
实际上没有。
如果文件已经有了哈希值,你可以比较这些哈希值。如果它们不同,那么你就可以得出这些文件不同的结论(然而,相同的哈希值并不意味着文件相同,因此你仍然需要进行逐字节比较)。
但是,哈希使用了文件中的所有字节,因此无论如何,你最终都必须逐字节读取文件。事实上,直接逐字节比较将比计算哈希快得多。这是因为哈希像逐字节比较一样读取了所有的字节,但哈希还要进行其他计算,这些计算会增加时间。此外,逐字节比较可以在第一对不相等字节时提前终止。
最后,你无法避免需要逐字节读取的需求。如果哈希值相同,并不意味着文件相同。在这种情况下,你仍然需要逐字节比较。
GetHashCode()
这样的东西。 - svick嗯,我不确定你是否可以在文件中写入时间戳。如果不能,你的唯一替代方法是比较文件的内容。
一个简单的方法是逐字节比较文件,但如果你要多次将文件与其他文件进行比较,你可以计算文件的哈希码并进行比较。
以下代码片段展示了如何实现:
public static string CalcHashCode(string filename)
{
FileStream stream = new FileStream(
filename,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite);
try
{
return CalcHashCode(stream);
}
finally
{
stream.Close();
}
}
public static string CalcHashCode(FileStream file)
{
MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
Byte[] hash = md5Provider.ComputeHash(file);
return Convert.ToBase64String(hash);
}
将文件读入流中,然后对流进行哈希处理。这样可以得到一个可靠的结果用于比较。
byte[] fileHash1, fileHash2;
using (SHA256Managed sha = new SHA256Managed())
{
fileHash1 = sha.ComputeHash(streamforfile1);
fileHash2 = sha.ComputeHash(streamforfile2);
}
for (int i = 0; (i < fileHash1.Length) && (i < fileHash2.Length); i++)
{
if (fileHash[i] != fileHash2[i])
{
//files are not the same
break;
}
}
将每个文件流通过MD5哈希器并比较哈希值。
d131dd02c5e6eec4693d9a0698aff95c 2fcab58712467eab4004583eb8fb7f89 55ad340609f4b30283e488832571415a 085125e8f7cdc99fd91dbdf280373c5b d8823e3156348f5bae6dacd436c919c6 dd53e2b487da03fd02396306d248cda0 e99f33420f577ee8ce54b67080a80d1e c69821bcb6a8839396f9652b6ff72a70
和 d131dd02c5e6eec4693d9a0698aff95c 2fcab50712467eab4004583eb8fb7f89 55ad340609f4b30283e4888325f1415a 085125e8f7cdc99fd91dbd7280373c5b d8823e3156348f5bae6dacd436c919c6 dd53e23487da03fd02396306d248cda0 e99f33420f577ee8ce54b67080280d1e c69821bcb6a8839396f965ab6ff72a70
具有相同的 MD5 哈希值。它们并不相等。 - jason