实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论