c#中比较两个文件

比较两个文件信息:
 public static bool isValidFileContent(string filePath1, string filePath2)
        {
            //创建一个哈希算法对象
            using (HashAlgorithm hash = HashAlgorithm.Create())
            {
                using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open))
                {
                    byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根据文本得到哈希码的字节数组
                    byte[] hashByte2 = hash.ComputeHash(file2);
                    string str1 = BitConverter.ToString(hashByte1);//将字节数组装换为字符串
                    string str2 = BitConverter.ToString(hashByte2);
                    return (str1 == str2);//比较哈希码
                }
            }
        }

 private void ToCompare_Click(object sender, EventArgs e)
        {
            // ReadTxtContent(@"C:\Users\Administrator\Desktop\1.txt");
            string filePath1 = @"D:\1\1.txt";
            string filePath2 = @"D:\2\新建文本文档.txt";
            bool valid = isValidFileContent(filePath1, filePath2);
            Console.WriteLine(valid.ToString());
            //Thread t1 = new Thread(new ThreadStart(ThreadLine1));
            //t1.IsBackground=true;
            //t1.Start();
            // progressBar1.Visible = true;
            // DownLoadFile("http://mirrors.tuna.tsinghua.edu.cn/apache//httpcomponents/httpclient/binary/httpcomponents-client-5.0-beta1-bin.tar.gz", "httpcomponents-client-5.0-beta1-bin.tar.gz",progressBar1);
            // CopyDirectory(@"D:\1", @"D:\2");
            if (valid.ToString().Equals("False"))
            {
               
                Form2 form = new Form2();
                form.ShowDialog();
                        
            }
            else
            {
                MessageBox.Show("无新版本");
                
            }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值