在 C# 中比较两个文件

发布于 2024-12-12 18:41:04 字数 102 浏览 0 评论 0原文

我想比较 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

紅太極 2024-12-19 18:41:04

根据您的目标,您可以查看 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);
}

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:

// 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);
}
桃酥萝莉 2024-12-19 18:41:04

我只是想知道是否有一种快速的方法可以做到这一点,而无需手动进入并读取文件。

并不真地。

如果文件带有哈希值,您可以比较哈希值,如果它们不同,您可以断定文件不同(但是,相同的哈希值并不意味着文件相同,因此您将还是要逐字节比较)。

但是,哈希使用文件中的所有字节,因此无论如何,您在某些时候都必须逐字节读取文件。事实上,直接逐字节比较会比计算哈希更快。这是因为哈希会读取所有字节,就像逐字节比较一样,但哈希会执行一些其他计算,从而增加时间。此外,逐字节比较可以在第一对不相等字节处提前终止。

最后,您无法避免逐字节读取的需要。如果哈希值相等,并不意味着文件相等。在这种情况下,你仍然需要逐字节比较。

I was just wondering if there is a fast way to do this without having to manually go in and read the file.

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.

攀登最高峰 2024-12-19 18:41:04

好吧,我不确定你是否可以在文件中写入时间戳。如果没有,您唯一的选择是比较文件的内容。

一种简单的方法是逐字节比较文件,但如果您要将一个文件与其他文件进行多次比较,您可以计算文件的哈希码并进行比较。

下面的代码片段展示了如何做到这一点:

    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);
    }

如果您要将一个文件与其他文件进行多次比较,您可以保存文件哈希并进行比较。对于单次比较,逐字节比较效果更好。当文件更改时,您还需要重新计算哈希,但如果您要进行大量比较(多次),我建议使用哈希方法。

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:

    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);
    }

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.

萌辣 2024-12-19 18:41:04

如果文件名相同,并且文件大小相同,那么,不,不检查内容就无法知道它们是否具有不同的内容。

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.

讽刺将军 2024-12-19 18:41:04

将文件读入流,然后对流进行哈希处理。这应该会给你一个可靠的比较结果。

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; 
        }
    }

Read the file into a stream, then hash the stream. That should give you a reliable result for comparing.

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; 
        }
    }
爱*していゐ 2024-12-19 18:41:04

如果它们不是编译文件,则使用 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/

淑女气质 2024-12-19 18:41:04

将每个文件流传递给 MD5 哈希器并比较哈希值。

pass each file stream through an MD5 hasher and compare the hashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文