C#MD5判断文件是否修改

对比md5值和原来文件是否一样,判断文件是否被i修改过。

public  string GetMD5HashFromFile(string fileName)
{
    try
    {
        FileStream file = new FileStream(fileName, FileMode.Open);
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(file);
        file.Close();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2"));
        }
        return sb.ToString();
    }
    catch (Exception ex)
    {
        throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
    }
}

 

posted @ 2020-07-14 09:47  ike_li  阅读(888)  评论(0编辑  收藏  举报