一個(gè)簡(jiǎn)單的文件MD5碼自動(dòng)計(jì)算比較器(附源碼)
主要對(duì) 多線程更新 winform 不是特別清楚,繞來(lái)繞去,搞得很暈乎,主要代碼如下, 還請(qǐng)各位大俠多多指點(diǎn),謝謝!
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 = "請(qǐng)選擇文件";
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 = "請(qǐng)先重新選擇文件!";
return;
}
FileStream fs = null;
try {
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
} catch (SystemException) {
textBox_result.Text = "文件打開(kāi)錯(cuò)誤,請(qǐng)重新選擇文件!";
return;
}
//對(duì)于大于 100M 的文件啟用多線程
if (fs.Length > 100L * 1024 * 1024) {
string message = "文件已經(jīng)超過(guò) 100M ,需要較長(zhǎng)的計(jì)算時(shí)間。\n軟件將啟動(dòng)后臺(tái)線程進(jìn)行處理。是否繼續(xù)?";
string caption = "文件較大";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
if (MessageBox.Show(message, caption, buttons) == System.Windows.Forms.DialogResult.No) {
fs.Close();
textBox_result.Text = "文件較大,未計(jì)算。";
return;
}
textBox_result.Text = "正在計(jì)算中,請(qǐng)稍候......";
button_calc.Enabled = false;
button_file.Enabled = false;
Thread thread = new Thread(new ParameterizedThreadStart(calcMD5));
thread.Start(fs);
} else {
calcMD5(fs);
}
}
//建立一個(gè) object 參數(shù)的函數(shù),是為了處理線程調(diào)用中,使用參數(shù)的問(wèn)題。
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 = "計(jì)算結(jié)果框中不是MD5碼,請(qǐng)先進(jìn)行計(jì)算!";
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碼 不匹配,文件已被修改,請(qǐng)小心!";
}
}
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 項(xiàng)目下載:http://xiazai.jb51.net/201302/yuanma/FileMD5_jb51.net.rar
相關(guān)文章
如何判斷?.NET?Core?應(yīng)用程序以管理員身份運(yùn)行的
這篇文章主要介紹了如何判斷?.NET?Core?應(yīng)用程序是以管理員身份運(yùn)行的,我們需要知道當(dāng)前程序是否以管理員身份運(yùn)行,以便執(zhí)行一些需要特殊權(quán)限的操作,下面為我們就來(lái)學(xué)習(xí)具體的方法吧,需要的朋友可以參考一下2022-03-03
ASP.NET沒(méi)有魔法_ASP.NET MVC 模型驗(yàn)證方法
下面小編就為大家分享一篇ASP.NET沒(méi)有魔法_ASP.NET MVC 模型驗(yàn)證方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
.NET實(shí)現(xiàn)WebSocket服務(wù)端即時(shí)通信實(shí)例
本篇文章主要介紹了.NET實(shí)現(xiàn)即時(shí)通信,WebSocket服務(wù)端實(shí)例 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
ASP.NET Core WebAPI實(shí)現(xiàn)本地化(單資源文件)
這篇文章主要介紹了ASP.NET Core WebAPI實(shí)現(xiàn)本地化(單資源文件),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Asp.net中把Excel數(shù)據(jù)存儲(chǔ)至SQL Server中的具體實(shí)現(xiàn)方法
Asp.net中把Excel數(shù)據(jù)存儲(chǔ)至SQL Server中的具體實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-06-06
Asp.net mvc驗(yàn)證用戶登錄之Forms實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Asp.net mvc驗(yàn)證用戶登錄之Forms實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
一個(gè).net 壓縮位圖至JPEG的實(shí)例代碼
這篇文章介紹了.net 壓縮位圖至JPEG的實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
ASP.NET數(shù)據(jù)庫(kù)編程之處理文件訪問(wèn)許可
ASP.NET數(shù)據(jù)庫(kù)編程之處理文件訪問(wèn)許可...2006-09-09
ASP.NET網(wǎng)站管理系統(tǒng)退出 清除瀏覽器緩存,Session的代碼
管理系統(tǒng)退出登陸后,將網(wǎng)址重新輸入后還會(huì)看到用戶登陸后的界面,為了解決這個(gè)問(wèn)題,我采用了以下方法,需要的朋友可以參考下2012-05-05
.NET C#創(chuàng)建WebService服務(wù)簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了.NET C# 創(chuàng)建WebService服務(wù)簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

