C#判断两个文件是否一样

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/y1535623813/article/details/85046966

1、使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否一致。    

2、 在比较文件内容的时候可以采用好几种方法。例如,检查文件的某一特定部分是否一致;如果愿意,你甚至可以逐字节读取文件,逐字节进行比较。这两种方法都是可以的,但在某些情况下,还是使用哈希码算法更为方便。

哈希算法将任意长度的二进制值映射为固定长度的较小二进制值,这个小的二进制值称为哈希值。

该算法为一个文件生成一个小的(通常约为20字节)二进制“指纹”(binary fingerprint)。从统计学角度看,不同的文件不可能生成相同的哈希码。事实上,即使是一个很小的改动(比如,修改了源文件中的一个bit),也会有50%的几率来改变哈希码中的每一个bit。因此,哈希码常常用于数据安全方面。要生成一个哈希码,你必须首先创建一个HashAlgorithm对象,而这通常是调用HashAlgorithm.Create方法来完成的;然后调用HashAlgorithm.ComputeHash方法,它会返回一个存储哈希码的字节数组。

如何获取文件的基本信息:

 可以使用FileInfo类的相关属性
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径); 
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo; 
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数)
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合;
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;  

利用哈希值判断两个文件内容是否相同  代码如下:

只需要建立一个控制台应用程序即可。

此方法已经测试 可用

  public static void CompareFile()
        {
            string p_1 = @"C:\Users\Administrator\Desktop\a\a.docx";
            string p_2 = @"C:\Users\Administrator\Desktop\a\b.docx";

            //计算第一个文件的哈希值
            var hash = System.Security.Cryptography.HashAlgorithm.Create();
            var stream_1 = new System.IO.FileStream(p_1, System.IO.FileMode.Open);
            byte[] hashByte_1 = hash.ComputeHash(stream_1);
            stream_1.Close();
            //计算第二个文件的哈希值
            var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
            byte[] hashByte_2 = hash.ComputeHash(stream_2);
            stream_2.Close();

            //比较两个哈希值
            if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
            {
                MessageBox.Show("两个文件相等");
            }
            else
            {
                MessageBox.Show("两个文件不相等");
            }
        }

个人归纳出比较两个文件内容是否完全一样的方法,详情如下:

一.逐一比较字节数

private bool CompareFile(string firstFile, string secondFile)
{
     if (!File.Exists(firstFile) || !File.Exists(secondFile)) {
         return false;
     }
     if (firstFile == secondFile) {
         return true;
     }
     int firstFileByte = 0;
     int secondFileByte = 0;
     FileStream secondFileStream = new FileStream(secondFile, FileMode.Open);
     FileStream firstFileStream = new FileStream(firstFile, FileMode.Open);
     if (firstFileStream.Length != secondFileStream.Length) {
         firstFileStream.Close();
         secondFileStream.Close();
         return false;
     }
     do
     {
         firstFileByte = firstFileStream.ReadByte();
         secondFileByte = secondFileStream.ReadByte();
     } while ((firstFileByte == secondFileByte) && (firstFileByte != -1)) ;
     firstFileStream.Close();
     secondFileStream.Close();
     return (firstFileByte == secondFileByte);
}
二.逐行比较内容

private bool CompareFileEx(string firstFile, string secondFile)
{
    if (!File.Exists(firstFile) || !File.Exists(secondFile)) {
        return false;
    }
    if (firstFile == secondFile) {
        return true;
    }
    string firstFileLine = string.Empty;
    string secondFileLine = string.Empty;
    StreamReader secondFileStream = new StreamReader(secondFile, Encoding.Default);
    StreamReader firstFileStream = new StreamReader(firstFile, Encoding.Default);
    do
    {
        firstFileLine = firstFileStream.ReadLine();
        secondFileLine = secondFileStream.ReadLine();
    } while ((firstFileLine == secondFileLine) && (firstFileLine != null));
    firstFileStream.Close();
    secondFileStream.Close();
    return (firstFileLine == secondFileLine);
}
三.比较哈希码

private bool CompareFileExEx(string firstFile, string secondFile)
{
    if (!File.Exists(firstFile) || !File.Exists(secondFile)) {
        return false;
    }
    if (firstFile == secondFile) {
        return true;
    }
    using (HashAlgorithm hash = HashAlgorithm.Create())
    {
        using (FileStream firstStream = new FileStream(firstFile, FileMode.Open), 
              secondStream = new FileStream(secondFile, FileMode.Open))
        {
            byte[] firstHashByte = hash.ComputeHash(firstStream);
            byte[] secondHashByte = hash.ComputeHash(secondStream);
            string firstString = BitConverter.ToString(firstHashByte); 
            string secondString = BitConverter.ToString(secondHashByte);
            return (firstString == secondString);
        }
    }
}
三种方法各有利弊,可根据具体情况具体分析,欢迎大家提出建议。
--------------------- 
作者:csdn_zhangchunfeng 
来源:CSDN 
原文:https://blog.csdn.net/csdn_zhangchunfeng/article/details/81093196 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/y1535623813/article/details/85046966
今日推荐