欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个简单的文件MD5码自动计算比较器(附源码)

程序员文章站 2024-03-03 19:12:46
主要对 多线程更新 winform 不是特别清楚,绕来绕去,搞得很晕乎,主要代码如下, 还请各位大侠多多指点,谢谢! 复制代码 代码如下:using system;&nb...

主要对 多线程更新 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);
         }
     }
 }

完整的 vs2010 项目下载:http://xiazai.jb51.net/201302/yuanma/filemd5_jb51.net.rar