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

获取文件的MD5码(C#)

时间:2014-05-09 19:43:27      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   ext   color   c   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
namespace Test
{
    public class MD5Code
    {
        /// <summary>
        /// 获取文件的MD5码
        /// </summary>
        /// <param name="fileName">传入的文件名(含路径及后缀名)</param>
        /// <returns></returns>
        public string GetMD5HashFromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, System.IO.FileMode.Open);
                MD5 md5 = new 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("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }
    }
}

 

获取文件的MD5码(C#),布布扣,bubuko.com

获取文件的MD5码(C#)

标签:blog   class   code   ext   color   c   

原文地址:http://www.cnblogs.com/jiao28/p/3718970.html

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