c#中比较两个文件
比較兩個(gè)文件信息:public static bool isValidFileContent(string filePath1, string filePath2){//創(chuàng)建一個(gè)哈希算法對(duì)象using (HashAlgorithm hash = HashAlgorithm.Create()){using (FileStream file1 = new FileStream(filePath1, FileMode.Open), file2 = new FileStream(filePath2, FileMode.Open)){byte[] hashByte1 = hash.ComputeHash(file1);//哈希算法根據(jù)文本得到哈希碼的字節(jié)數(shù)組byte[] hashByte2 = hash.ComputeHash(file2);string str1 = BitConverter.ToString(hashByte1);//將字節(jié)數(shù)組裝換為字符串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("無(wú)新版本");}}
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("無(wú)新版本");}}
總結(jié)
- 上一篇: 2018计算机网络原理自考4月,2018
- 下一篇: Unity热更系列--C#访问XLua的