C#比较两文件内容是否完全一样

本文介绍了使用C#进行文件内容比较的三种方法:逐一比较字节数、逐行比较内容和比较哈希码。每种方法都提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                     C#比较两文件内容是否完全一样

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

一.逐一比较字节数

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

三种方法各有利弊,可根据具体情况具体分析,欢迎大家提出建议。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值