A simple file MD5 code automatically calculates the comparator (with source code)

Source: Internet
Author: User

I am not particularly clear about how to update winform with multiple threads. The Code is as follows. Thank you for your advice!

Copy codeThe Code is as follows:
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 = "select a file ";
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 = "Please reselect the file first! ";
Return;
}

FileStream fs = null;
Try {
Fs = new FileStream (file, FileMode. Open, FileAccess. Read );
} Catch (SystemException ){
TextBox_result.Text = "An error occurred while opening the file. Please reselect the file! ";
Return;
}

// Enable multithreading for files larger than 100 MB
If (fs. Length> 100L * 1024*1024 ){

String message = "the file has exceeded 100 MB, which takes a long time to calculate. \ N software will start the background thread for processing. Continue? ";
String caption = "large files ";
MessageBoxButtons buttons = MessageBoxButtons. YesNo;

If (MessageBox. Show (message, caption, buttons) = System. Windows. Forms. DialogResult. No ){
Fs. Close ();
TextBox_result.Text = "the file is large and not computed. ";
Return;
}
TextBox_result.Text = "computing... please wait ......";
Button_calc.Enabled = false;
Button_file.Enabled = false;

Thread thread = new Thread (new ParameterizedThreadStart (calizer 5 ));
Thread. Start (fs );

} Else {
Cal1_5 (fs );
}
}

// Create an object Parameter Function to handle the use of parameters in thread calls.
Private void cal00005 (object fs ){
Cal12005 (FileStream) fs );
}

// Delegate used by the Invoke Function
Delegate void updateWindows (byte [] result );

Private void cal00005 (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 = "the calculated result box is not an MD5 code. Perform calculation first! ";
Return;
}

If (textBox_md5.Text.Trim (). ToUpper (). Equals (result. ToUpper ())){
TextBox_compare.Visible = true;
TextBox_compare.BackColor = Color. LightGreen;
TextBox_compare.Text = "the MD5 code has been matched and the file has not been modified. You can use it with confidence! ";
} Else {
TextBox_compare.Visible = true;
TextBox_compare.BackColor = Color. Red;
TextBox_compare.Text = "the MD5 Code does not match. The file has been modified. Please be careful! ";
}
}

Private void showFilesize (long size ){

Float d_size;
String unit = "Byte ";

If (size> 1024*1024*1024) {// display greater than 1G
D_size = size/(float) (1024*1024*1024 );
Unit = "GB ";
} Else {
If (size> 1024*1024) {// display greater than 1 M
D_size = size/(float) (1024*1024 );
Unit = "MB ";
} Else {
If (size> 1024) {// display greater than 1 K
D_size = size/(float) (1024 );
Unit = "KB ";
} Else {
D_size = size;
}
}
}
TextBox_filesize.Text = string. Format ("{0: F} {1} ({2: N0} bytes)", d_size, unit, size );
}
}
}

Complete VS2010 project download: http://xiazai.jb51.net/201302/yuanma/FileMD5_jb51.net.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.