佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 一個簡單的文件MD5碼自動計算比較器(附源碼)

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

熱門標簽:電子圍欄 團購網(wǎng)站 Linux服務器 銀行業(yè)務 Mysql連接數(shù)設置 服務器配置 阿里云 科大訊飛語音識別系統(tǒng)

主要對 多線程更新 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 = "文件已經(jīng)超過 100M ,需要較長的計算時間。\n軟件將啟動后臺線程進行處理。是否繼續(xù)?";
                 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 參數(shù)的函數(shù),是為了處理線程調用中,使用參數(shù)的問題。
         private void calcMD5(object fs) {
             calcMD5((FileStream)fs);
         }

         // Invoke 函數(shù)需要使用的委托
         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}字節(jié) )", d_size, unit, size);
         }
     }
 }

完整的 VS2010 項目下載:http://xiazai.jb51.net/201302/yuanma/FileMD5_jb51.net.rar

標簽:蚌埠 衡水 廣元 衢州 江蘇 大理 萍鄉(xiāng) 棗莊

巨人網(wǎng)絡通訊聲明:本文標題《一個簡單的文件MD5碼自動計算比較器(附源碼)》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    漳浦县| 贵德县| 陆河县| 屏东县| 靖州| 平定县| 安乡县| 保德县| 阿坝| 尼木县| 汤阴县| 宜君县| 樟树市| 巧家县| 天镇县| 巴塘县| 西青区| 潜山县| 高尔夫| 张家港市| 辰溪县| 宁阳县| 茌平县| 广东省| 团风县| 临邑县| 古田县| 台北县| 名山县| 金华市| 江山市| 会泽县| 罗源县| 西吉县| 徐闻县| 吕梁市| 宾阳县| 灌云县| 博客| 新乡市| 合阳县|