//-----------------------------------------------------------------------
//
// Copyright (c) ParanoidMike. All rights reserved.
//
//-----------------------------------------------------------------------
namespace ParanoidMike
{
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
///
/// Reusable functions for many uses.
///
public static class Utility
{
#region Variables
///
/// Variable for the HKCU hive, to be used with functions that use the RegistryKey class.
///
private static RegistryKey hkcu = Registry.CurrentUser;
///
/// Variable for the HKLM hive, to be used with functions that use the RegistryKey class.
///
private static RegistryKey hklm = Registry.LocalMachine;
///
/// Variable for identifying the major version of the Operating System.
///
private static int osVersion = -1; // contains os major version number
#endregion
#region Public Methods
///
/// Retrieves any Registry value that uses the REGBINARY data type.
///
///
/// Specifies whether to retrieve from the HKCU hive:
/// - if True, retrieves from HKCU
/// - if False, retrieves from HKLM
///
///
/// The relative path (within the specified hive) to the Registry Key where the value is found.
///
///
/// The Registry value whose data is retrieved.
///
///
/// The data in the specified Registry value.
///
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;
}
}
}