通过事件机制,增加了计算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.csdn.net/chuangxin/article/details/5333376
本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1931427,如需转载请自行联系原作者