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; } } }