Ejemplo n.º 1
0
        /// <summary>
        /// Gets a 128-bit hash of the specified file.
        /// </summary>
        /// <param name="path">Path to the file</param>
        /// <param name="hash">Integer array of length 4 which receives the
        /// four 32-bit parts of the hash value.</param>
        /// <exception cref="FileNotFoundException">the file does not exist or
        /// could not be read</exception>
        /// <remarks><p>
        /// Win32 MSI API:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msigetfilehash.asp">MsiGetFileHash</a>
        /// </p></remarks>
        public static void GetFileHash(string path, int[] hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            uint[] tempHash = new uint[5];
            tempHash[0] = 20;
            uint ret = NativeMethods.MsiGetFileHash(path, 0, tempHash);

            if (ret != 0)
            {
                if (ret == (uint)NativeMethods.Error.FILE_NOT_FOUND ||
                    ret == (uint)NativeMethods.Error.ACCESS_DENIED)
                {
                    throw new FileNotFoundException(null, path);
                }
                else
                {
                    throw InstallerException.ExceptionFromReturnCode(ret);
                }
            }

            for (int i = 0; i < 4; i++)
            {
                hash[i] = unchecked ((int)tempHash[i + 1]);
            }
        }