A simple file MD5 code automatic calculation comparator (with source code) _ Practical Skills

Source: Internet
Author: User
Tags md5

Mainly to multithreading update WinForm is not particularly clear, around to go around, make very dizzy, the main code is as follows, but also please you heroes a lot of guidance, thank you!

Copy Code code 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 = "Please select 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 select File First!";
Return
}

FileStream fs = null;
try {
FS = new FileStream (file, FileMode.Open, FileAccess.Read);
} catch (SystemException) {
Textbox_result. Text = "File open error, please select file again!";
Return
}

Enable multithreading for files larger than 100M
if (fs. Length > 100L * 1024 * 1024) {

String message = "The file has exceeded 100M, requiring a longer calculation time." \ nthe software will start a background thread for processing. Do you want to continue? ";
String caption = "Larger file";
MessageBoxButtons buttons = Messageboxbuttons.yesno;

if (MessageBox.Show (message, caption, buttons) = = System.Windows.Forms.DialogResult.No) {
Fs. Close ();
Textbox_result. Text = "File is large, not calculated." ";
Return
}
Textbox_result. Text = "In calculation, please wait ...";
Button_calc. Enabled = false;
Button_file. Enabled = false;

Thread thread = new Thread (new Parameterizedthreadstart (calcMD5));
Thread. Start (FS);

} else {
CalcMD5 (FS);
}
}

A function that establishes an object parameter is used to handle the problem of using parameters in the thread invocation.
private void CalcMD5 (Object fs) {
CalcMD5 ((FileStream) FS);
}

The delegate that the Invoke function needs to use
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 = "The calculation results box is not MD5 code, please do the calculation first!" ";
Return
}

if (textbox_md5. Text.trim (). ToUpper (). Equals (result. ToUpper ()) {
Textbox_compare. Visible = true;
Textbox_compare. BackColor = Color.lightgreen;
Textbox_compare. Text = "MD5 code has been matched, file has not been modified, can be assured of use!" ";
} else {
Textbox_compare. Visible = true;
Textbox_compare. BackColor = color.red;
Textbox_compare. Text = "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) {//greater than 1G display
D_size = size/(float) (1024 * 1024 * 1024);
Unit = "GB";
} else {
if (Size > 1024 * 1024) {//greater than 1M display
D_size = size/(float) (1024 * 1024);
Unit = "MB";
} else {
if (Size > 1024) {//greater than 1K display
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.