/// <summary>
        /// Gets the decoded Windows product key based on the Windows edition.
        /// </summary>
        /// <param name="windowsEdition">The Windows edition currently being run.</param>
        /// <returns>A string containing the Windows product key.</returns>
        public static string GetProductKey(WindowsEditions windowsEdition)
        {
            byte[] digitalProductId = null;
            string results = string.Empty;

            try
            {
                digitalProductId = KeyFinder.GetRegistryDigitalProductId(windowsEdition);
            }
            catch (Exception ex)
            {
                results = ex.Message;
            }

            if (digitalProductId != null)
            {
                results = KeyFinder.DecodeProductKey(digitalProductId);
            }
            else
            {
                results = "Product key could not be retrieved.";
            }

            return results;
        }
        /// <summary>
        /// Gets the Windows product key from the registry in encoded form.
        /// </summary>
        /// <param name="windowsEdition">The Windows edition of the system being used.</param>
        /// <returns>A byte array containing the value of the encrypted Windows product key.</returns>
        private static byte[] GetRegistryDigitalProductId(WindowsEditions windowsEdition)
        {
            byte[] digitalProductId = null;

            string digitalProductIdKeyName = GetDigitalProductIdKeyNameForWindowsEdition(windowsEdition);

            RegistryKey registry = Registry.LocalMachine.OpenSubKey(regKeyPath, false);
            {
                digitalProductId = registry.GetValue(digitalProductIdKeyName) as byte[];
            }

            registry.Close();

            if (digitalProductId == null)
            {
                uint type;
                int bufferSize = 2048;
                digitalProductId = new byte[2048];

                if (RegOpenKeyEx(hiveKeyLocalMachine, regKeyPath, 0, keyQueryValue | keyWow6464Key, out regKeyHandle) == success)
                {
                    RegQueryValueEx(regKeyHandle, digitalProductIdKeyName, 0, out type, digitalProductId, ref bufferSize);
                }

                if (RegOpenKeyEx(hiveKeyLocalMachine, regKeyPath, 0, keyQueryValue | keyWow6432Key, out regKeyHandle) == success)
                {
                    RegQueryValueEx(regKeyHandle, digitalProductIdKeyName, 0, out type, digitalProductId, ref bufferSize);
                }
            }

            return digitalProductId;
        }
 /// <summary>
 /// Gets the correct Product ID Key Name in the registry for the specified Windows edition.
 /// </summary>
 /// <param name="edition">The Windows edition of the system currently in use.</param>
 /// <returns>A string containing the correct Digital Product ID Key name.</returns>
 private static string GetDigitalProductIdKeyNameForWindowsEdition(WindowsEditions edition)
 {
     switch (edition)
     {
         case WindowsEditions.Windows7Professional:
             return "DigitalProductId4";
         default:
             return "DigitalProductId";
     }
 }