/// <summary> /// 计算指定文件的MD5值 /// </summary> /// <param name="fileName">指定文件的完全限定名称</param> /// <returns>返回值的字符串形式</returns> public static String ComputeMD5(String fileName) { var hashMD5 = String.Empty; //检查文件是否存在,如果文件存在则进行计算,否则返回空值 if (File.Exists(fileName)) { using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { //计算文件的MD5值 var calculator = MD5.Create(); var buffer = calculator.ComputeHash(fs); calculator.Clear(); //将字节数组转换成十六进制的字符串形式 var stringBuilder = new StringBuilder(); for (var i = 0; i < buffer.Length; i++) { stringBuilder.Append(buffer[i].ToString("x2")); } hashMD5 = stringBuilder.ToString(); } } return hashMD5; }