文件的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);
}
}
}