【C#】MD5 小程序编写

本文介绍了一种使用C#在Windows Forms应用中计算文件MD5值的方法,并通过事件机制实现了计算过程中状态提示的功能。该方法利用了异步编程来提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过事件机制,增加了计算MD5过程提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
using  System.IO;
 
namespace  WindowsFormsApplication13
{
     public  partial  class  Form1 : Form
     {
         public  delegate  void  delegateMD( object  sender, EventArgs e);
         public  event  delegateMD openfileEvent;
         public  event  delegateMD finishEvent;
 
         public  Form1()
         {
             InitializeComponent();
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
 
         }
 
         private  async  void  button1_Click( object  sender, EventArgs e)
         {
             openfileEvent += ( object  obj, EventArgs e1) => {
                 label1.Text =  "正在计算MD5值,请稍等。。。" ; };
 
             finishEvent += ( object  obj, EventArgs e1) =>
               {
                   label1.Text =  "" ;
               };
 
             using  (OpenFileDialog dialog =  new  OpenFileDialog())
             {
                 if  (dialog.ShowDialog() == DialogResult.OK)
                 {
                     openfileEvent( this , e);
                     String fileName = dialog.FileName;
                     this .textBox1.Text =  "" ;
                     //this.txtSH1.Text = "";
                     //
                     this .textBox1.Text = await getMD5HashAsync(fileName);
                     //this.txtSH1.Text = GetMD5Hash(fileName);
                     finishEvent( this , e);
                 }
             }
         }
         //计算文件的MD5码
         private  Task< string > getMD5HashAsync( string  pathName)
         {
             return  Task.Run< string >( () =>
             {
                 string  strResult =  "" ;
                 string  strHashData =  "" ;
 
                 byte [] arrbytHashValue;
                 System.IO.FileStream oFileStream =  null ;
 
                 System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                            new  System.Security.Cryptography.MD5CryptoServiceProvider();
 
                 try
                 {
                     oFileStream =  new  FileStream(pathName, FileMode.Open,
                           FileAccess.Read,FileShare.ReadWrite);
                     arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
                     oFileStream.Close();
                     //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                     strHashData = BitConverter.ToString(arrbytHashValue);
                     //替换-
                     strHashData = strHashData.Replace( "-" "" );
                     strResult = strHashData;
                 }
                 catch  (System.Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
 
                 return  strResult;
             });
         }
     }
 
     
}

异步执行代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
using  System.IO;
 
namespace  WindowsFormsApplication13
{
     public  partial  class  Form1 : Form
     {
         public  Form1()
         {
             InitializeComponent();
         }
 
         private  void  Form1_Load( object  sender, EventArgs e)
         {
 
         }
 
         private  async  void  button1_Click( object  sender, EventArgs e)
         {
             using  (OpenFileDialog dialog =  new  OpenFileDialog())
             {
                 if  (dialog.ShowDialog() == DialogResult.OK)
                 {
                     String fileName = dialog.FileName;
                     this .textBox1.Text =  "" ;
                     //this.txtSH1.Text = "";
                     //
                     this .textBox1.Text = await getMD5HashAsync(fileName);
                     //this.txtSH1.Text = GetMD5Hash(fileName);
                 }
             }
         }
         //计算文件的MD5码
         private  Task< string > getMD5HashAsync( string  pathName)
         {
             return  Task.Run< string >( () =>
             {
                 string  strResult =  "" ;
                 string  strHashData =  "" ;
 
                 byte [] arrbytHashValue;
                 System.IO.FileStream oFileStream =  null ;
 
                 System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                            new  System.Security.Cryptography.MD5CryptoServiceProvider();
 
                 try
                 {
                     oFileStream =  new  FileStream(pathName, FileMode.Open,
                           FileAccess.Read,FileShare.ReadWrite);
                     arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值
                     oFileStream.Close();
                     //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                     strHashData = BitConverter.ToString(arrbytHashValue);
                     //替换-
                     strHashData = strHashData.Replace( "-" "" );
                     strResult = strHashData;
                 }
                 catch  (System.Exception ex)
                 {
                     MessageBox.Show(ex.Message);
                 }
 
                 return  strResult;
             });
         }
     }
 
     
}



参考链接:

http://blog.youkuaiyun.com/chuangxin/article/details/5333376






      本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1931427,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值