编程语言
首页 > 编程语言> > C#获取文件的MD5码

C#获取文件的MD5码

作者:互联网

/// <summary>
/// 获取文件MD5值
/// </summary>
/// <param name="file">文件绝对路径</param>
/// <returns>MD5值</returns>
public static string GetMD5HashFromFile(string file)
{
try
{
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fileStream);
fileStream.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);
}
}

标签:C#,获取,new,sb,retVal,fileStream,FileStream,MD5
来源: https://www.cnblogs.com/hs-47/p/15988175.html