计算一个文件的MD5值


 1      /// <summary>
 2         /// 计算文件的MD5值
 3         /// </summary>
 4         public static string md5file(string file) {
 5             try {
 6                 FileStream fs = new FileStream(file, FileMode.Open);
 7                 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
 8                 byte[] retVal = md5.ComputeHash(fs);
 9                 fs.Close();
10 
11                 StringBuilder sb = new StringBuilder();
12                 for (int i = 0; i < retVal.Length; i++) {
13                     sb.Append(retVal[i].ToString("x2"));
14                 }
15                 return sb.ToString();
16             } catch (Exception ex) {
17                 throw new Exception("md5file() fail, error:" + ex.Message);
18             }
19         }

  以上这个函数是在Unity项目中的SimpleFramework热更新框架摘出来的,希望在其他项目中不用再自己写了,特此标记。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM