Skip to content

Instantly share code, notes, and snippets.

@liulixiang1988
Created May 12, 2015 02:55
Show Gist options
  • Save liulixiang1988/45c38bccf4a1b7dab61f to your computer and use it in GitHub Desktop.
Save liulixiang1988/45c38bccf4a1b7dab61f to your computer and use it in GitHub Desktop.
Calculate File's Md5 计算文件的MD5
//It's very simple:
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
return md5.ComputeHash(stream);
}
}
//(I believe that actually the MD5 implementation used doesn't need to be disposed, but I'd probably still do so anyway.)
//How you compare the results afterwards is up to you; you can convert the byte array to base64 for example, or compare the bytes directly. (Just be aware that arrays don't override Equals. Using base64 is simpler to get right, but slightly less efficient if you're really only interested in comparing the hashes.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment