2005/02/25 | 生成/检测文件的MD5
类别(.Net) | 评论(0) | 阅读(295) | 发表于 12:54
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Security.Cryptography;

namespace FileMD5
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox txtMD5;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox txtPath;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TabPage tabPage3;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.TextBox txtMD5File;
		private System.Windows.Forms.TextBox txtLOG;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.button3 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.txtMD5 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.txtPath = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.txtLOG = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.txtMD5File = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.tabPage3 = new System.Windows.Forms.TabPage();
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.tabPage3.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Controls.Add(this.tabPage3);
			this.tabControl1.Location = new System.Drawing.Point(8, 8);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(416, 176);
			this.tabControl1.TabIndex = 7;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.button3);
			this.tabPage1.Controls.Add(this.button2);
			this.tabPage1.Controls.Add(this.txtMD5);
			this.tabPage1.Controls.Add(this.label2);
			this.tabPage1.Controls.Add(this.button1);
			this.tabPage1.Controls.Add(this.txtPath);
			this.tabPage1.Controls.Add(this.label1);
			this.tabPage1.Location = new System.Drawing.Point(4, 21);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(408, 151);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "创建";
			// 
			// button3
			// 
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button3.Location = new System.Drawing.Point(344, 72);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(56, 23);
			this.button3.TabIndex = 13;
			this.button3.Text = "保存(&S)";
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button2.Location = new System.Drawing.Point(283, 72);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 23);
			this.button2.TabIndex = 12;
			this.button2.Text = "复制(&C)";
			// 
			// txtMD5
			// 
			this.txtMD5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtMD5.Location = new System.Drawing.Point(49, 72);
			this.txtMD5.Name = "txtMD5";
			this.txtMD5.ReadOnly = true;
			this.txtMD5.Size = new System.Drawing.Size(224, 21);
			this.txtMD5.TabIndex = 11;
			this.txtMD5.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(14, 76);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(32, 16);
			this.label2.TabIndex = 10;
			this.label2.Text = "MD5:";
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.button1.Location = new System.Drawing.Point(320, 22);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 23);
			this.button1.TabIndex = 9;
			this.button1.Text = "浏览(&B)";
			this.button1.Click += new System.EventHandler(this.button1_Click_1);
			// 
			// txtPath
			// 
			this.txtPath.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
			this.txtPath.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtPath.Location = new System.Drawing.Point(49, 22);
			this.txtPath.Name = "txtPath";
			this.txtPath.ReadOnly = true;
			this.txtPath.Size = new System.Drawing.Size(248, 21);
			this.txtPath.TabIndex = 8;
			this.txtPath.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(12, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 16);
			this.label1.TabIndex = 7;
			this.label1.Text = "文件:";
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.txtLOG);
			this.tabPage2.Controls.Add(this.button4);
			this.tabPage2.Controls.Add(this.txtMD5File);
			this.tabPage2.Controls.Add(this.label5);
			this.tabPage2.Location = new System.Drawing.Point(4, 21);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(408, 151);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "校验";
			// 
			// txtLOG
			// 
			this.txtLOG.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtLOG.Location = new System.Drawing.Point(8, 56);
			this.txtLOG.Multiline = true;
			this.txtLOG.Name = "txtLOG";
			this.txtLOG.ReadOnly = true;
			this.txtLOG.Size = new System.Drawing.Size(392, 80);
			this.txtLOG.TabIndex = 3;
			this.txtLOG.Text = "";
			// 
			// button4
			// 
			this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.button4.Location = new System.Drawing.Point(344, 16);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(56, 23);
			this.button4.TabIndex = 2;
			this.button4.Text = "打开(&O)";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// txtMD5File
			// 
			this.txtMD5File.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
			this.txtMD5File.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtMD5File.Location = new System.Drawing.Point(64, 16);
			this.txtMD5File.Name = "txtMD5File";
			this.txtMD5File.Size = new System.Drawing.Size(272, 21);
			this.txtMD5File.TabIndex = 1;
			this.txtMD5File.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 21);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(40, 16);
			this.label5.TabIndex = 0;
			this.label5.Text = "文件:";
			// 
			// tabPage3
			// 
			this.tabPage3.Controls.Add(this.linkLabel1);
			this.tabPage3.Controls.Add(this.label4);
			this.tabPage3.Controls.Add(this.label3);
			this.tabPage3.Controls.Add(this.pictureBox1);
			this.tabPage3.Location = new System.Drawing.Point(4, 21);
			this.tabPage3.Name = "tabPage3";
			this.tabPage3.Size = new System.Drawing.Size(408, 151);
			this.tabPage3.TabIndex = 2;
			this.tabPage3.Text = "关于";
			// 
			// linkLabel1
			// 
			this.linkLabel1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.linkLabel1.Font = new System.Drawing.Font("Verdana", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.linkLabel1.LinkColor = System.Drawing.Color.Red;
			this.linkLabel1.Location = new System.Drawing.Point(104, 72);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.Size = new System.Drawing.Size(184, 16);
			this.linkLabel1.TabIndex = 3;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "http://allinhands.vicp.net";
			this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Verdana", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(104, 24);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 2;
			this.label4.Text = "FileMD5 0.01";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Verdana", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(104, 48);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 1;
			this.label3.Text = "Allinhands";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(24, 24);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(64, 64);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(432, 189);
			this.Controls.Add(this.tabControl1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "File MD5";
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.tabPage3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}


		private string MD5(string FilePath)
		{
			FileStream fs = new FileStream(FilePath,FileMode.Open,FileAccess.Read);
			MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
			byte [] md5byte = md5.ComputeHash(fs);
			string md5string="";
			int i,j;
			foreach (byte b in md5byte)
			{
				i = Convert.ToInt32(b);
				j = i >> 4;
				md5string+=Convert.ToString(j,16);
				j = ((i << 4) & 0x00ff) >> 4;
				md5string+=Convert.ToString(j,16);
			}
			return md5string;
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.txtMD5.SelectAll();
			this.txtMD5.Copy();
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog dl = new SaveFileDialog();
			dl.Filter = "MD5(*.md5)|*.md5";
			FileInfo fi = new FileInfo(this.txtPath.Text);
			dl.DefaultExt = "md5";
			dl.AddExtension=true;
			dl.InitialDirectory = fi.Directory.ToString();
			dl.FileName = fi.Name.Replace(fi.Extension,"");
			if(dl.ShowDialog()==DialogResult.OK)
			{
				FileStream fs = new FileStream(dl.FileName,FileMode.OpenOrCreate,FileAccess.Write);
				StreamWriter m_streamWriter = new StreamWriter(fs);
				m_streamWriter.Write(this.txtMD5.Text+" "+fi.Name);
				m_streamWriter.Flush();
				m_streamWriter.Close(); 
				fs.Close();
			}
		}

		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start("http://allinhands.5d.cn"); 
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog dl = new OpenFileDialog();
			dl.Filter = "MD5(*.md5)|*.md5";
			if(dl.ShowDialog()==DialogResult.OK)
			{
				this.txtMD5File.Text = dl.FileName;
			}
			else
			{
				return;
			}
			this.txtLOG.Text ="";
			this.txtLOG.Text +="正在读取文件...\r\n";
			FileStream fs = new FileStream(this.txtMD5File.Text,FileMode.Open,FileAccess.Read);
			StreamReader m_streamReader = new StreamReader(fs);
			this.txtLOG.Text +="正在分析文件...\r\n";
			string values = m_streamReader.ReadLine();
			m_streamReader.Close();
			fs.Close();
			string md5 = values.Split(" ".ToCharArray())[0];
			string file =  values.Split(" ".ToCharArray())[1];
			FileInfo fi = new FileInfo(this.txtMD5File.Text);
			FileInfo fii = new FileInfo(fi.Directory+"\\"+file);
			if(!fii.Exists)
			{
				this.txtLOG.Text +="文件"+fii.FullName+"不存在\r\n";
				return;
			}
			else
			{
				string md5string = MD5(fi.Directory+"\\"+file);
				if(md5string==md5)
				{
					this.txtLOG.Text +="检测成功,MD5一致\r\n";
				}
				else
				{
					this.txtLOG.Text +="检测失败,MD5不一致\r\n";
				}
			}
		}

		private void button1_Click_1(object sender, System.EventArgs e)
		{
			OpenFileDialog dl = new OpenFileDialog();
			dl.Title = "选择一个文件";
			dl.Filter = "全部文件|*.*";
			if(dl.ShowDialog()==DialogResult.OK)
			{
				this.txtPath.Text = dl.FileName;
				this.txtMD5.Text = MD5(this.txtPath.Text);
			}

		}


	}
}
0

评论Comments

日志分类
首页[102]
.Net[11]
VBScript[1]
JScript[51]
XML[3]
HTML&CSS[9]
ASP[8]
ActiveX[4]
Software[10]
Other[5]