C# 获取文件MD5校验码

时间:2015-05-05 18:19:02   收藏:0   阅读:349
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;


    public class MD5Code
    {
        /// <summary>
        /// Get 文件的MD5校验码
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        public static string GetMD5HashFromFile(string fileName)
        {
           
            
                var file = new FileStream(fileName, System.IO.FileMode.Open);
                var md5 = new MD5CryptoServiceProvider();
                var bytes= md5.ComputeHash(file);
                file.Close();
                var sb = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(bytes[i].ToString("x2"));
                }
                return sb.ToString();
        
          }
    }

 

原文:http://www.cnblogs.com/gaobing/p/4479361.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!