一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - 一個簡單的文件MD5碼自動計算比較器(附源碼)

一個簡單的文件MD5碼自動計算比較器(附源碼)

2019-10-25 11:35asp.net教程網 ASP.NET教程

一直在玩 WOW ,發現網上的 MD5 計算工具都沒有自動比較功能,每次下載更新計算后,都要自己一個一個字母核對,比較麻煩。 最近開始學習 C# ,用 .NET ,做了一個簡單的文件MD5碼自動計算比較器。

主要對 多線程更新 winform 不是特別清楚,繞來繞去,搞得很暈乎,主要代碼如下, 還請各位大俠多多指點,謝謝!

 

復制代碼代碼如下:

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Security.Cryptography;
 using System.IO;
 using System.Threading;

 namespace FileMD5 {
     public partial class MainForm : Form {
         public MainForm() {
             InitializeComponent();
         }

         private void button_file_Click(object sender, EventArgs e) {
             OpenFileDialog fileDialog = new OpenFileDialog();
             fileDialog.Title = "請選擇文件";
             fileDialog.RestoreDirectory = true;

             if (fileDialog.ShowDialog() == DialogResult.OK) {
                 textBox_file.Text = fileDialog.FileName;
                 textBox_result.Text = "";
                 FileInfo file = new FileInfo(fileDialog.FileName);
                 fileSzie = file.Length;
                 showFilesize(fileSzie);
             }

         }

         private void button_exit_Click(object sender, EventArgs e) {
             this.Close();
             this.Dispose();
         }

         private void button_check_Click(object sender, EventArgs e) {
             checkResult();
         }

         MD5 md5 = (MD5)CryptoConfig.CreateFromName("MD5");
         long fileSzie = 0;

         private void button_calc_Click(object sender, EventArgs e) {
             string file = textBox_file.Text;

             if (file.Length == 0) {
                 textBox_result.Text = "請先重新選擇文件!";
                 return;
             }

             FileStream fs = null;
             try {
                 fs = new FileStream(file, FileMode.Open, FileAccess.Read);
             } catch (SystemException) {
                 textBox_result.Text = "文件打開錯誤,請重新選擇文件!";
                 return;
             }

             //對于大于 100M 的文件啟用多線程
             if (fs.Length > 100L * 1024 * 1024) {

                 string message = "文件已經超過 100M ,需要較長的計算時間。\n軟件將啟動后臺線程進行處理。是否繼續?";
                 string caption = "文件較大";
                 MessageBoxButtons buttons = MessageBoxButtons.YesNo;

                 if (MessageBox.Show(message, caption, buttons) == System.Windows.Forms.DialogResult.No) {
                     fs.Close();
                     textBox_result.Text = "文件較大,未計算。";
                     return;
                 }
                 textBox_result.Text = "正在計算中,請稍候......";
                 button_calc.Enabled = false;
                 button_file.Enabled = false;

                 Thread thread = new Thread(new ParameterizedThreadStart(calcMD5));
                 thread.Start(fs);

             } else {
                 calcMD5(fs);
             }
         }

         //建立一個 object 參數的函數,是為了處理線程調用中,使用參數的問題。
         private void calcMD5(object fs) {
             calcMD5((FileStream)fs);
         }

         // Invoke 函數需要使用的委托
         delegate void updateWindows(byte[] result);

         private void calcMD5(FileStream fs) {
             byte[] md5byte = md5.ComputeHash(fs);

             if (this.InvokeRequired) {
                 this.Invoke(new updateWindows(showResult), md5byte);
             } else {
                 showResult(md5byte);
             }
             fs.Close();
         }

         private void showResult(byte[] md5byte) {
             int i, j;
             StringBuilder sb = new StringBuilder(32);
             foreach (byte b in md5byte) {
                 i = Convert.ToInt32(b);
                 j = i >> 4;
                 sb.Append(Convert.ToString(j, 16));
                 j = ((i << 4) & 0x00ff) >> 4;
                 sb.Append(Convert.ToString(j, 16));
             }

             String result = sb.ToString().ToUpper();

             textBox_result.Text = result;
             button_calc.Enabled = true;
             button_file.Enabled = true;
             checkResult();

         }

         private void checkResult() {

             string result = textBox_result.Text;

             if (textBox_md5.Text.Length == 0) {
                 textBox_compare.Text = "";
                 textBox_compare.Visible = false;
                 return;
             }

             if(result.Length != 32 ) {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.Pink;
                 textBox_compare.Text = "計算結果框中不是MD5碼,請先進行計算!";
                 return;
             }

             if (textBox_md5.Text.Trim().ToUpper().Equals(result.ToUpper())) {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.LightGreen;
                 textBox_compare.Text = "MD5碼 已匹配,文件未被修改,可放心使用!";
             } else {
                 textBox_compare.Visible = true;
                 textBox_compare.BackColor = Color.Red;
                 textBox_compare.Text = "MD5碼 不匹配,文件已被修改,請小心!";
             }
         }

         private void showFilesize(long size) {

             float d_size;
             string unit = "Byte";

             if (size > 1024 * 1024 * 1024) {    //大于 1G 的顯示
                 d_size = size / (float)(1024 * 1024 * 1024);
                 unit = "GB";
             } else {
                 if (size > 1024 * 1024) {    //大于 1M 的顯示
                     d_size = size / (float)(1024 * 1024);
                     unit = "MB";
                 } else {
                     if (size > 1024) {    //大于 1K 的顯示
                         d_size = size / (float)(1024);
                         unit = "KB";
                     } else {
                         d_size = size;
                     }
                 }
             }
             textBox_filesize.Text = string.Format(" {0:F} {1} ( {2:N0}字節 )", d_size, unit, size);
         }
     }
 }

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 荡女淫春2未删减版 | 国内体内she精视频免费 | 四虎永久成人免费 | 精品亚洲午夜久久久久 | 性派对videos18party | 精品性影院一区二区三区内射 | 日本不卡在线观看免费v | 高h全肉np触手 | 性xxxx欧美高清 | 天天射久久| 欧美a级v片不卡在线观看 | 精品国产中文字幕在线视频 | h动态图男女啪啪27报 | 五月天在线视频观看 | 欧美日韩一级视频 | 亚洲人和日本人hd | 被老头肉至怀孕小说 | 午夜精品久久久 | 日韩精品一区二区三区视频 | 国产精品一区二区三 | 日韩免费观看成第15集 | 99热这里有免费国产精品 | 亚洲 欧美 国产 综合久久 | 国自产精品手机在线视频 | 女人日男人| 日本视频在线观看 | 9 1 视频在线 | 九九九精品视频 | 精品视频一区二区三区免费 | 国产一级视频久久 | 欧美精品一区二区三区免费 | 羞羞麻豆国产精品1区2区3区 | 免费日韩 | 日本春菜花在线中文字幕 | 国产成人cao在线 | 91传媒在线观看 | 91青青国产在线观看免费 | 欧美亚洲国产另类在线观看 | 青草热久精品视频在线观看 | 欧美亚洲桃花综合 | 精品综合久久久久久8888 |