Skip to content

Instantly share code, notes, and snippets.

@camark
Created December 20, 2016 04:04
Show Gist options
  • Save camark/6a59094ad594b204f241ae2e9193f7c1 to your computer and use it in GitHub Desktop.
Save camark/6a59094ad594b204f241ae2e9193f7c1 to your computer and use it in GitHub Desktop.
计算文件MD5值
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment