1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace FileToMD5
- {
- public partial class Form_MD5 : Form
- {
- public Form_MD5()
- {
- InitializeComponent();
- }
- private void btn_md5_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.InitialDirectory = "D:\\";
- try
- {
- if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- txtFilePath.Text = ofd.FileName;
- //创建一个文件对象
- FileInfo EzoneFile = new FileInfo(txtFilePath.Text);
- //打开文件流
- FileStream EzoneStream = EzoneFile.OpenRead();
- //EzoneStream.Position = 0;
- long size = EzoneStream.Length;
- byte[] array = new byte[size];//文件
- EzoneStream.Read(array, 0, array.Length);
- string md5 = GetMD5HashFromFile(array);
- txtMd5.Text = md5;
- EzoneStream.Close();
- EzoneStream = null;
- EzoneFile = null;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
-
- }
- public static string GetMD5HashFromFile(byte[] filedatas)
- {
- try
- {
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(filedatas);
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- return sb.ToString();
- }
- catch (Exception ex)
- {
- //errorinfo = ex.Message.ToString();
- return "";
- }
- }
- }
- }
|