在 C# 中比较两个文件
我想比较 C# 中的两个文件,看看它们是否不同。它们具有相同的文件名,并且不同时大小完全相同。我只是想知道是否有一种快速的方法可以做到这一点,而无需手动进入并读取文件。
谢谢
I want to compare two files in C# and see if they are different. They have the same file names and they are the exact same size when different. I was just wondering if there is a fast way to do this without having to manually go in and read the file.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您的目标,您可以查看 Diff.NET
这里有一个简单的文件比较功能:
Depending on how far you're looking to take it, you can take a look at Diff.NET
Here's a simple file comparison function:
并不真地。
如果文件带有哈希值,您可以比较哈希值,如果它们不同,您可以断定文件不同(但是,相同的哈希值并不意味着文件相同,因此您将还是要逐字节比较)。
但是,哈希使用文件中的所有字节,因此无论如何,您在某些时候都必须逐字节读取文件。事实上,直接逐字节比较会比计算哈希更快。这是因为哈希会读取所有字节,就像逐字节比较一样,但哈希会执行一些其他计算,从而增加时间。此外,逐字节比较可以在第一对不相等字节处提前终止。
最后,您无法避免逐字节读取的需要。如果哈希值相等,并不意味着文件相等。在这种情况下,你仍然需要逐字节比较。
Not really.
If the files came with hashes, you could compare the hashes, and if they are different you can conclude the files are different (same hashes, however, does not mean the files are the same and so you will still have to do a byte by byte comparison).
However, hashes use all the bytes in the file, so no matter what, you at some point have to read the files byte for byte. And in fact, just a straight byte by byte comparison will be faster than computing a hash. This is because a hash reads all the bytes just like comparing byte-by-byte does, but hashes do some other computations that add time. Additionally, a byte-by-byte comparison can terminate early on the first pair of non-equal bytes.
Finally, you can not avoid the need for a byte-by-byte read. If the hashes are equal, that doesn't mean the files are equal. In this case you still have to compare byte-by-byte.
好吧,我不确定你是否可以在文件中写入时间戳。如果没有,您唯一的选择是比较文件的内容。
一种简单的方法是逐字节比较文件,但如果您要将一个文件与其他文件进行多次比较,您可以计算文件的哈希码并进行比较。
下面的代码片段展示了如何做到这一点:
如果您要将一个文件与其他文件进行多次比较,您可以保存文件哈希并进行比较。对于单次比较,逐字节比较效果更好。当文件更改时,您还需要重新计算哈希,但如果您要进行大量比较(多次),我建议使用哈希方法。
Well, I'm not sure if you can in the file write timestamps. If not, your unique alternative, is comparing the content of the files.
A simple approach is comparing the files byte-to-byte, but if you're going to compare a file several times with others, you can calculate the hashcode of the files and compare it.
The following code snippet shows how you can do it:
If you're going to compare a file with others more that one time, you can save the file hash and compare it. For a single comparison, the byte-to-byte comparison is better. You need also to recompute hash when the file changes, but if you're going to do massive comparisons (more than one time), I recommend using the hash approach.
如果文件名相同,并且文件大小相同,那么,不,不检查内容就无法知道它们是否具有不同的内容。
If the filenames are the same, and the file sizes are the same, then, no, there is no way to know if they have different content without examining the content.
将文件读入流,然后对流进行哈希处理。这应该会给你一个可靠的比较结果。
Read the file into a stream, then hash the stream. That should give you a reliable result for comparing.
如果它们不是编译文件,则使用 KDiff 或 WinMerge 等比较工具。它将突出显示它们的不同之处。
http://kdiff3.sourceforge.net/
http://winmerge.org/
If they are not complied files then use a diff tool like KDiff or WinMerge. It will highlight were they are different.
http://kdiff3.sourceforge.net/
http://winmerge.org/
将每个文件流传递给 MD5 哈希器并比较哈希值。
pass each file stream through an MD5 hasher and compare the hashes.