file check sum

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hi,

My app records some sound to a file and then copies it to a network drive.
We recently had some issues when file was copied corrupted besause of some network issues.
So i am going to make some copied file test against the original file to make sure it was copied correctly.

Are the any standard .NET remedies for that? Obviousely, comparing just size is not working sometimes,
and I also woudn't want to do a "by-byte" file comparing.

Any ideas would be highly appreciated!!!

Thank you,
Andrey
 
MuZZy said:
My app records some sound to a file and then copies it to a network
drive. We recently had some issues when file was copied corrupted
besause of some network issues. So i am going to make some copied
file test against the original file to make sure it was copied
correctly.

Are the any standard .NET remedies for that? Obviousely, comparing
just size is not working sometimes, and I also woudn't want to do a
"by-byte" file comparing.

Any ideas would be highly appreciated!!!

You could use an MD5 hash, Crc32 or something similar to check the file
against.

As it happens, I have an Adler32 implementation you could use in my
MiscUtil library, if you want:
http://www.pobox.com/~skeet/csharp/miscutil

Using MD5 would probably be easier though, as you wouldn't need the
extra library. It would take more computation, but I wouldn't expect
that to be a problem.
 
Hi,

My app records some sound to a file and then copies it to a
network drive. We recently had some issues when file was copied
corrupted besause of some network issues. So i am going to make
some copied file test against the original file to make sure it
was copied correctly.

Are the any standard .NET remedies for that? Obviousely,
comparing just size is not working sometimes, and I also woudn't
want to do a "by-byte" file comparing.

Andrey,

A good way to compare the physical contents of two files is to use a
checksum. Below is a method I use to calculate the MD5 checksum for
a given file:

using System.IO;
using System.Security.Cryptography;
using System.Text;

...

private static string GetMD5ChecksumForFile(string filename)
{
if (filename == null)
throw new ArgumentNullException("The 'filename' parameter cannot be null.");

if (!File.Exists(filename))
throw new ArgumentException(string.Format("Filename '{0}' does not exist.", filename));

using (FileStream fstream = new FileStream(filename, FileMode.Open))
{
byte[] hash = new MD5CryptoServiceProvider().ComputeHash(fstream);

// Convert the byte array to a printable string.
StringBuilder sb = new StringBuilder(32);
foreach (byte hex in hash)
sb.Append(hex.ToString("X2"));

return sb.ToString().ToUpper();
}
}
 
Back
Top