Retrieves any Registry value that uses the REGBINARY data type. : Registry « Development Class « C# / C Sharp






Retrieves any Registry value that uses the REGBINARY data type.

        
//-----------------------------------------------------------------------
// <copyright file="Utility.cs" company="ParanoidMike">
//     Copyright (c) ParanoidMike. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace ParanoidMike
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using Microsoft.Win32;

    /// <summary>
    /// Reusable functions for many uses.
    /// </summary>
    public static class Utility
    {


        #region Variables

        /// <summary>
        /// Variable for the HKCU hive, to be used with functions that use the RegistryKey class.
        /// </summary>
        private static RegistryKey hkcu = Registry.CurrentUser;

        /// <summary>
        /// Variable for the HKLM hive, to be used with functions that use the RegistryKey class.
        /// </summary>
        private static RegistryKey hklm = Registry.LocalMachine;

        /// <summary>
        /// Variable for identifying the major version of the Operating System.
        /// </summary>
        private static int osVersion = -1; // contains os major version number

        #endregion

        #region Public Methods


        /// <summary>
        /// Retrieves any Registry value that uses the REGBINARY data type.
        /// </summary>
        /// <param name="userHive">
        /// Specifies whether to retrieve from the HKCU hive:
        /// - if True, retrieves from HKCU
        /// - if False, retrieves from HKLM
        /// </param>
        /// <param name="subKey">
        /// The relative path (within the specified hive) to the Registry Key where the value is found.
        /// </param>
        /// <param name="valueName">
        /// The Registry value whose data is retrieved.
        /// </param>
        /// <returns>
        /// The data in the specified Registry value.
        /// </returns>
        public static byte[] GetRegistryValue(
            bool   userHive, 
            string subKey, 
            string valueName)
        {
            ////const string SubKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\EFS\\CurrentKeys";
            ////const string ValueName = "CertificateHash";

            RegistryKey registrySubKey;
            byte[] registryValue; // Declare a variable to hold the returned Registry value

            if (userHive)
            {
                registrySubKey = hkcu.OpenSubKey(subKey);
            }
            else
            {
                registrySubKey = hklm.OpenSubKey(subKey);
            }

            registryValue = (byte[]) registrySubKey.GetValue(valueName, null);

            // NOTE: Previously I tried to derive a string that can be compared to X509Certificate2.GetCertHashString()
            // e.g. "C480C669C22270BACD51E65C6AC28596DFF93D0D"
            // Note: I tried this conversion code I found on the 'Net http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1656747&SiteId=1, but couldn't get it to work
            registrySubKey.Close();

            if (userHive)
            {
                hkcu.Close();
            }
            else
            {
                hklm.Close();
            }

            return registryValue;
        }

    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Registry.LocalMachine
2.Write a Text and DWord Value to the Registry
3.Enumerating Registry Keys
4.Retrieve the CPU Type and Speed from the Registry
5.Use GetValue and SetValue to get and save value to Registry
6.Get the Registry key found for CurrentUser
7. Accessing the Registry
8.Open a SubKey in Registry
9.Get Registry value Get Registry value
10.Registry File Association
11.Check Registry to see if it is installed
12.Get/Set User Registry
13.Copy Registry
14.Registry Utils
15.Writes a Registry value to the Registry.
16.Get IIS version