Sometimes we have the necessity to verify that the file we downloded wasn’t corrupted. A way to do this it’s to generate a MD5 checksum and verify the file integrity.
The purpose of this check is to establish that the file has not been modified by someone.
public string GetMD5HashFromFile(string fileName) { FileStream file = new FileStream(fileName, FileMode.Open); MD5 md5 = new 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")); } string checksum = sb.ToString(); return checksum; }