C#获取文件MD5

来源:互联网 发布:两年后索隆的实力 知乎 编辑:程序博客网 时间:2024/04/29 22:09


C#获取文件MD5码:


<span style="font-size:18px;"> static void Main(string[] args)        {           string code = GetMD5HashFromFile("123.pdf");        }        private static 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);            }        }</span>


0 0
原创粉丝点击