Posted By

wearetherock on 11/19/09


Tagged

hash md5


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
khouser


Get MD5 Hash of any file


 / Published in: C#
 

URL: http://www.codeproject.com/KB/files/dt_file_hasher.aspx

  1. public static string GetMD5Hash(string pathName)
  2. {
  3. string strResult = "";
  4. string strHashData = "";
  5.  
  6. byte[] arrbytHashValue;
  7. System.IO.FileStream oFileStream = null;
  8.  
  9. System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher=
  10. new System.Security.Cryptography.MD5CryptoServiceProvider();
  11.  
  12. try
  13. {
  14. oFileStream = GetFileStream(pathName);
  15. arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
  16. oFileStream.Close();
  17.  
  18. strHashData = System.BitConverter.ToString(arrbytHashValue);
  19. strHashData = strHashData.Replace("-", "");
  20. strResult = strHashData;
  21. }
  22. catch(System.Exception ex)
  23. {
  24. System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
  25. System.Windows.Forms.MessageBoxButtons.OK,
  26. System.Windows.Forms.MessageBoxIcon.Error,
  27. System.Windows.Forms.MessageBoxDefaultButton.Button1);
  28. }
  29.  
  30. return(strResult);
  31. }

Report this snippet  

You need to login to post a comment.