// 获取Res文件夹下所有文件的相对路径和MD5值
string[] files = Directory.GetFiles(resPath, "*", SearchOption.AllDirectories);
StringBuilder versions = new StringBuilder();
for (int i = 0, len = files.Length; i < len; i++)
{
string filePath = files[i];
string extension = filePath.Substring(files[i].LastIndexOf("."));
if (extension == ".unity3d")
{
string relativePath = filePath.Replace(resPath, "").Replace("\\", "/");
string md5 = MD5File(filePath);
versions.Append(relativePath).Append(",").Append(md5).Append("\n");
}
}
// 生成配置文件
FileStream stream = new FileStream(resPath + "version.txt", FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
public static string MD5File(string file)
{
try
{
FileStream fs = new FileStream(file, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();//创建对象
byte[] retVal = md5.ComputeHash(fs); // 获取md5值, 是一个Hash字节数组
fs.Close(); //关闭文件流
StringBuilder sb = new StringBuilder(); //使用字符流节省性能
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2")); //x2:以16进制转换
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("md5file() fail, error:" + ex.Message);
}
}