Ejemplo n.º 1
0
        static public string GetFileHash(string FilePath)
        {
            try
            {
                FileStream infile;
                // Open the file as a FileStream object.
                infile = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[infile.Length];
                // Read the file to ensure it is readable.
                int count = infile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    infile.Close();
                    throw new PSLibException(string.Format("Unable to read data from file: {0}", FilePath));
                }
                infile.Close();

#if DEBUG
                string hash = CRC.GetMd5Sum(buffer);

                if (FilePath.Contains("Pictures"))
                {
                    System.Diagnostics.Trace.TraceInformation("Hash: " + hash);
                }

                return(hash);
#else
                return(CRC.GetMd5Sum(buffer));
#endif
            }
            catch (System.OutOfMemoryException e)
            {
                throw new Exception(e.Message, e);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }