对于文件的唯一标示信息,不应该知识版本,日期等,常常大家会看到Md5的信息,这里提供下C#中获取文件的Md5信息,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#region 文件MD5信息 /// <summary> /// 获取文件的MD5信息 /// </summary> /// <param name="fileName">文件名</param> /// <returns>MD5信息</returns> public 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++) { //---转化为小写的16进制 sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (Exception ex) { throw new Exception("GetMD5Hash fail," + ex.Message); } } #endregion |