您的位置:首页 > 移动开发 > Unity3D

【Unity开发】获取文件的MD5

2016-01-07 14:10 393 查看
文件的MD5值是独一无二的,所以当MD5值改变时说明文件改变了

近期做移动端的断点续传正是运用到这一特性。
using UnityEngine;
using System.Collections;
using System.Net.NetworkInformation;
using System.IO;
using System;
using System.Text;

public class Test : MonoBehaviour {

// Use this for initialization
void Start()
{
try
{
//指定文集为根目录下的Test.cs
FileStream fs = new FileStream(Application.dataPath + "/Test.cs", FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fs);
fs.Close();

StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}

print(sb);

}
catch (Exception ex)
{

throw new Exception("md5file() fail, error:" + ex.Message);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: