MD5常用于文件校验,避免文件在人为、软件读写、网络传输等过程中发生改变,造成异常。
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace YYX
{
static class Md5Helper
{
public static string GetMd5HashFromFile(string fileName)
{
using (var fileStream = new FileStream(fileName, FileMode.Open))
{
var md5 = new MD5CryptoServiceProvider();
var hash = md5.ComputeHash(fileStream);
var hexHash = string.Concat(hash.Select(item => item.ToString("X2")));
return hexHash;
}
}
}
}