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


一直在玩 WOW ,發現網上的 MD5 計算工具都沒有自動比較功能,每次下載更新計算后,都要自己一個一個字母核對,比較麻煩。

 最近開始學習 C# ,用 .NET ,做了一個簡單的文件MD5碼自動計算比較器。

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

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Security.Cryptography;
 10 using System.IO;
 11 using System.Threading;
 12 
 13 namespace FileMD5 {
 14     public partial class MainForm : Form {
 15         public MainForm() {
 16             InitializeComponent();
 17         }
 18 
 19         private void button_file_Click(object sender, EventArgs e) {
 20             OpenFileDialog fileDialog = new OpenFileDialog();
 21             fileDialog.Title = "請選擇文件";
 22             fileDialog.RestoreDirectory = true;
 23 
 24             if (fileDialog.ShowDialog() == DialogResult.OK) {
 25                 textBox_file.Text = fileDialog.FileName;
 26                 textBox_result.Text = "";
 27                 FileInfo file = new FileInfo(fileDialog.FileName);
 28                 fileSzie = file.Length;
 29                 showFilesize(fileSzie);
 30             }
 31 
 32         }
 33 
 34         private void button_exit_Click(object sender, EventArgs e) {
 35             this.Close();
 36             this.Dispose();
 37         }
 38 
 39         private void button_check_Click(object sender, EventArgs e) {
 40             checkResult();
 41         }
 42 
 43         MD5 md5 = (MD5)CryptoConfig.CreateFromName("MD5");
 44         long fileSzie = 0;
 45 
 46         private void button_calc_Click(object sender, EventArgs e) {
 47             string file = textBox_file.Text;
 48 
 49             if (file.Length == 0) {
 50                 textBox_result.Text = "請先重新選擇文件!";
 51                 return;
 52             }
 53 
 54             FileStream fs = null;
 55             try {
 56                 fs = new FileStream(file, FileMode.Open, FileAccess.Read);
 57             } catch (SystemException) {
 58                 textBox_result.Text = "文件打開錯誤,請重新選擇文件!";
 59                 return;
 60             }
 61 
 62             //對於大於 100M 的文件啟用多線程
 63             if (fs.Length > 100L * 1024 * 1024) {
 64 
 65                 string message = "文件已經超過 100M ,需要較長的計算時間。\n軟件將啟動后台線程進行處理。是否繼續?";
 66                 string caption = "文件較大";
 67                 MessageBoxButtons buttons = MessageBoxButtons.YesNo;
 68 
 69                 if (MessageBox.Show(message, caption, buttons) == System.Windows.Forms.DialogResult.No) {
 70                     fs.Close();
 71                     textBox_result.Text = "文件較大,未計算。";
 72                     return;
 73                 }
 74                 textBox_result.Text = "正在計算中,請稍候......";
 75                 button_calc.Enabled = false;
 76                 button_file.Enabled = false;
 77 
 78                 Thread thread = new Thread(new ParameterizedThreadStart(calcMD5));
 79                 thread.Start(fs);
 80 
 81             } else {
 82                 calcMD5(fs);
 83             }
 84         }
 85 
 86         //建立一個 object 參數的函數,是為了處理線程調用中,使用參數的問題。
 87         private void calcMD5(object fs) {
 88             calcMD5((FileStream)fs);
 89         }
 90 
 91         // Invoke 函數需要使用的委托
 92         delegate void updateWindows(byte[] result);
 93 
 94         private void calcMD5(FileStream fs) {
 95             byte[] md5byte = md5.ComputeHash(fs);
 96 
 97             if (this.InvokeRequired) {
 98                 this.Invoke(new updateWindows(showResult), md5byte);
 99             } else {
100                 showResult(md5byte);
101             }
102             fs.Close();
103         }
104 
105         private void showResult(byte[] md5byte) {
106             int i, j;
107             StringBuilder sb = new StringBuilder(32);
108             foreach (byte b in md5byte) {
109                 i = Convert.ToInt32(b);
110                 j = i >> 4;
111                 sb.Append(Convert.ToString(j, 16));
112                 j = ((i << 4) & 0x00ff) >> 4;
113                 sb.Append(Convert.ToString(j, 16));
114             }
115 
116             String result = sb.ToString().ToUpper();
117 
118             textBox_result.Text = result;
119             button_calc.Enabled = true;
120             button_file.Enabled = true;
121             checkResult();
122             
123         }
124 
125         private void checkResult() {
126 
127             string result = textBox_result.Text;
128 
129             if (textBox_md5.Text.Length == 0) {
130                 textBox_compare.Text = "";
131                 textBox_compare.Visible = false;
132                 return;
133             }
134             
135             if(result.Length != 32 ) {
136                 textBox_compare.Visible = true;
137                 textBox_compare.BackColor = Color.Pink;
138                 textBox_compare.Text = "計算結果框中不是MD5碼,請先進行計算!";
139                 return;
140             }
141 
142             if (textBox_md5.Text.Trim().ToUpper().Equals(result.ToUpper())) {
143                 textBox_compare.Visible = true;
144                 textBox_compare.BackColor = Color.LightGreen;
145                 textBox_compare.Text = "MD5碼 已匹配,文件未被修改,可放心使用!";
146             } else {
147                 textBox_compare.Visible = true;
148                 textBox_compare.BackColor = Color.Red;
149                 textBox_compare.Text = "MD5碼 不匹配,文件已被修改,請小心!";
150             }
151         }
152 
153         private void showFilesize(long size) {
154             
155             float d_size;
156             string unit = "Byte";
157 
158             if (size > 1024 * 1024 * 1024) {    //大於 1G 的顯示
159                 d_size = size / (float)(1024 * 1024 * 1024);
160                 unit = "GB";
161             } else {
162                 if (size > 1024 * 1024) {    //大於 1M 的顯示
163                     d_size = size / (float)(1024 * 1024);
164                     unit = "MB";
165                 } else {
166                     if (size > 1024) {    //大於 1K 的顯示
167                         d_size = size / (float)(1024);
168                         unit = "KB";
169                     } else {
170                         d_size = size;
171                     }
172                 }
173             }
174             textBox_filesize.Text = string.Format(" {0:F} {1} ( {2:N0}字節 )", d_size, unit, size);
175         }
176     }
177 }

 

完整的 VS2010 項目見:

http://files.cnblogs.com/jacs/FileMD5.rar

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM