ASP.Net/C# - How to validate PDF file is not corrupt after copy - Checksum?

sbornstein2
sbornstein2 used Ask the Experts™
on
Is there a way after I do a file.copy that I can check if the PDF file that transferred is not corrupt without opening the file?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The easiest way is to get the MD5 Checksum for the file before the copy and after the copy.
If the checksum's match then the file is fine, if they are different then there was a problem with the copy.

I've attached a subroutine for you to try out, it will generate the MD5 of a file.

you'll need to make sure you have these:

System.IO
System.Text
System.Security.Cryptography

protected string GetMD5HashFromFile(string fileName)
{
  FileStream file = new FileStream(fileName, FileMode.Open);
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();
  ASCIIEncoding enc = new ASCIIEncoding();
  return enc.GetString(retVal);
}

Open in new window

Author

Commented:
awesome thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial