获取文件Md5值

        private static string GetFileMD5(string filePath)
        {
            string _md5Value = string.Empty;
            try
            {
                if (System.IO.File.Exists(filePath))
                {
                    FileStream file = new FileStream(filePath, FileMode.Open);
                    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    byte[] retVal = md5.ComputeHash(file);
                    file.Close();
                    file.Dispose();
                    _md5Value= BitConverter.ToString(retVal).Replace("-", "");
                }
                return _md5Value;
            }
            catch (Exception ex)
            {
                //throw new Exception(string.Format("GetFileMD5_Error_{0}_{1}", DateTime.Now, ex.Message));
                return _md5Value;
            }
        }

 

posted @ 2016-11-04 12:19  灰色雨逸  阅读(266)  评论(0编辑  收藏  举报