MuZZy <le*******@yahoo.com> wrote in
news:ut********************@rcn.net:
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();
}
}
--
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/