C# 判断两个文件内容是否一致

该代码段定义了一个函数boolIsSameFile,用于比较两个文件file1和file2的内容是否一致。首先检查文件名是否相同,然后打开文件并逐字节读取,如果文件长度不同或读取到不同的字节,就返回false。当读完整个文件后,如果所有字节都相同,则返回true。

file1和file2是两个需要比较的文件

bool IsSameFile( string file1, string file2 )

{

if( file1 == file2 ) {

return true;

}

int file1byte = 0;

int file2byte = 0;

using( FileStream fs1 = new FileStream( file1, FileMode.Open ),

fs2 = new FileStream( file2, FileMode.Open ) ) {

if( fs1.Length != fs2.Length ) {

fs1.Close();

fs2.Close();

return false;

}

do {

file1byte = fs1.ReadByte();

file2byte = fs2.ReadByte();

}

while( ( file1byte == file2byte ) && ( file1byte != -1 ) );

fs1.Close();

fs2.Close();

}

return ( ( file1byte - file2byte ) == 0 );

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值