计算文件的MD5值
参数filePath为文件的绝对路径
public static string md5file(string filePath)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
int fsLen = (int)fs.Length;
byte[] heByte = new byte[fsLen];
int r = fs.Read(heByte, 0, heByte.Length);
byte[] retVal = md5.ComputeHash(heByte);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception)
{
return "";
}
}
计算文件大小:
string str = filePath;
long fileLen = new FileInfo(str).Length;
Debug.Log("fileLen =" + fileLen.ToString());