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.Threading.Tasks;
using System.Windows.Forms;
namespace FileMd5
{
public partial class GetMd5 : Form
{
public GetMd5()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1Md5.Text = "Get MD5";
}
/// <summary>
/// 获取文件MD5值
/// </summary>
/// <param name="fileName">文件绝对路径</param>
/// <returns>MD5值</returns>
public static string GetMD5(string fileName)
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
button1Md5.Text = "ing";
string strMd5 = GetMD5(textBox1Path.Text.ToString().Trim());
Clipboard.SetDataObject(strMd5);
textBox2Md5.Text = "已复制到剪切板 " + strMd5;
}
catch (Exception ex)
{
textBox2Md5.Text = "err";
}
finally
{
button1Md5.Text = "Get MD5";
}
}
}
}