A simple file MD5 code automatic calculation comparator (with source code)
- 2020-05-26 08:13:19
- OfStack
The main update of winform is not very clear about multi-threading, winding around, get very confused, the main code is as follows, also ask each hero to give directions, thank you!
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 reselect the file first !";
return;
}
FileStream fs = null;
try {
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
} catch (SystemException) {
textBox_result.Text = " File opening error , Please reselect the file !";
return;
}
// For more than 100M File to enable multithreading
if (fs.Length > 100L * 1024 * 1024) {
string message = " File has exceeded 100M , which requires a long calculation time. \n The software will start the background thread for processing. Do you want to continue? ";
string caption = " The file is bigger ";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
if (MessageBox.Show(message, caption, buttons) == System.Windows.Forms.DialogResult.No) {
fs.Close();
textBox_result.Text = " Large file, not counted. ";
return;
}
textBox_result.Text = " Please wait a moment while we calculate ......";
button_calc.Enabled = false;
button_file.Enabled = false;
Thread thread = new Thread(new ParameterizedThreadStart(calcMD5));
thread.Start(fs);
} else {
calcMD5(fs);
}
}
// To establish 1 a object Parameter functions are designed to handle the problem of using parameters in thread calls.
private void calcMD5(object fs) {
calcMD5((FileStream)fs);
}
// Invoke The delegate that the 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 = " Not in the result box MD5 Code, please calculate 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, the file has not been modified, can be assured to 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) { // Is greater than 1G The display of the
d_size = size / (float)(1024 * 1024 * 1024);
unit = "GB";
} else {
if (size > 1024 * 1024) { // Is greater than 1M The display of the
d_size = size / (float)(1024 * 1024);
unit = "MB";
} else {
if (size > 1024) { // Is greater than 1K The display of the
d_size = size / (float)(1024);
unit = "KB";
} else {
d_size = size;
}
}
}
textBox_filesize.Text = string.Format(" {0:F} {1} ( {2:N0} byte )", d_size, unit, size);
}
}
}
Download complete VS2010 project: http: / / xiazai ofstack. com / 201302 / yuanma FileMD5_ofstack com. rar