C#获取文件MD5值方法

https://www.cnblogs.com/Ruiky/archive/2012/04/16/2451663.html

 

 

private static string GetMD5HashFromFile(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(“GetMD5HashFromFile() fail,error:” + ex.Message);
 }
 }
posted @ 2018-08-15 21:28  LuoCore  阅读(464)  评论(0编辑  收藏  举报