码迷,mamicode.com
首页 > 其他好文 > 详细

文件MD5 并复制到剪切板

时间:2020-04-14 16:55:07      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:forms   names   pac   art   ons   uil   namespace   file   thread   

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";
            }
        }
    }
}

 

文件MD5 并复制到剪切板

标签:forms   names   pac   art   ons   uil   namespace   file   thread   

原文地址:https://www.cnblogs.com/2eggs/p/12698639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!