using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using Yaulw.Tools;
namespace Yaulw.Registry
{
#region RegKey Enums
///
/// Registry Root Key
///
public enum HKEYRoot
{
ClassesRoot,
CurrentUser,
LocalMachine,
}
///
/// Registry Sub Key
///
public enum KeySub
{
Software,
Custom
}
#endregion
///
/// Wrapper Class around Registry Functionallity,
/// allows for easy reading/writing to the registry, especially HKEY_CURRENT_USER\Software
/// ~If you use this class directly make sure to call dispose(),
/// otherwise, use the Public Static Helper Functions to do the work for you
///
public class RegKey : IDisposable
{
#region Private Members
private RegistryKey _RegKey;
private bool _disposed = false;
#endregion
#region Constructors
///
/// Use this to Open or Automatically Create the specified Key in CURRENT_USER\Software\Key.
///
/// Specify Key to Open
/// Set to true to Open, false to Create if not Exists
public RegKey(string Key, bool bReadOnly = true) : this(HKEYRoot.CurrentUser, KeySub.Software, Key, bReadOnly) {}
///
/// Use this to Open or Automatically Create a Registry Key
///
/// specify registry root
/// specify a sub or custom
/// key you want to open / create
/// true if you want to force only reading it
private RegKey(HKEYRoot root, KeySub sub, string Key, bool bReadOnly)
{
string KeyToOpen = String.Empty;
if (sub == KeySub.Custom)
KeyToOpen = Key;
else
KeyToOpen = sub.ToString() + "\\" + Key;
// Read Only Permission
RegistryKeyPermissionCheck permission = RegistryKeyPermissionCheck.ReadSubTree;
if (!bReadOnly)
permission = RegistryKeyPermissionCheck.ReadWriteSubTree;
// Open or Create, if it doesn't exist and we have writable set
switch (root)
{
case HKEYRoot.ClassesRoot:
if (bReadOnly)
_RegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(KeyToOpen);
else
_RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(KeyToOpen, permission);
break;
case HKEYRoot.CurrentUser:
if (bReadOnly)
_RegKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(KeyToOpen, permission);
else
_RegKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(KeyToOpen, permission);
break;
case HKEYRoot.LocalMachine:
if (bReadOnly)
_RegKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(KeyToOpen, permission);
else
_RegKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(KeyToOpen, permission);
break;
}
}
///
/// Finalizer
///
~RegKey()
{
Dispose(true);
}
#endregion
#region GetValue Registry Functions
///
/// Generic Registry KeyValue Getter Function
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// The Value to get from the Registry
/// Default value to use if nothing was retrieved * Error occured *
/// Value found or Default Value
public T GetVal(string strKeyValue, T DefaultValue)
{
T retVal = DefaultValue;
try
{
if (_RegKey != null)
{
object keyvalue = _RegKey.GetValue(strKeyValue);
string strvalue = string.Empty;
if (ObjTool.IsNotNullAndNotEmpty(keyvalue))
{
#region First Handle the Value as a string
// Handle Multi-Strings
if (keyvalue is object[])
{
string keyVal2 = string.Empty;
string[] strVal2 = (string[])keyvalue;
foreach (string str in strVal2)
{
keyVal2 += str;
}
strvalue = keyVal2;
}
else
{
// Handle Single-Strings
strvalue = keyvalue.ToString();
}
#endregion
// Now cast the Object Return type * Handle each object differently *
retVal = ObjTool.ConvertStringToObj(strvalue);
}
}
}
catch (Exception) { /* ignore */ }
return retVal;
}
///
/// Binary Registry KeyValue Getter Function
///
/// The Value to get from the Registry
/// an array of Bytes found in the Registry, or null if none found, or error occured
public byte[] GetVal(string strKeyValue)
{
byte[] retVal = null;
try
{
string StringValue = GetVal(strKeyValue, String.Empty);
if (!String.IsNullOrEmpty(StringValue))
{
Byte[] ByteValue = new Byte[StringValue.Length];
for (int i = 0; i < ByteValue.Length; ++i)
{
string strByte = System.Convert.ToString(StringValue[i]);
ByteValue[i] = byte.Parse(strByte);
}
retVal = ByteValue;
}
}
catch (Exception) { /* ignore */ }
return retVal;
}
#endregion
#region SetValue Registry Functions
///
/// Generic Registry KeyValue Setter Function
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// The Value to write to in the Registry
/// The Value to set to the Registry
/// true if successful, false otherwise
public bool SetVal(string strKeyValue, T Value)
{
bool bRetVal = false;
try
{
if (_RegKey != null)
{
if (Value is System.Int32)
{
_RegKey.SetValue(strKeyValue, Value, RegistryValueKind.DWord);
}
else if (Value is System.Int64)
{
_RegKey.SetValue(strKeyValue, Value, RegistryValueKind.QWord);
}
else
{
_RegKey.SetValue(strKeyValue, Value, RegistryValueKind.String);
}
bRetVal = true;
}
}
catch (Exception) { /* ignore */ }
return bRetVal;
}
///
/// Generic Registry KeyValue Setter Function
///
/// The Value to write to in the Registry
/// A binary array of bytes to write to the Registry
/// true if successful, false otherwise
public bool SetVal(string strKeyValue, byte[] ByteValue)
{
try
{
_RegKey.SetValue(strKeyValue, ByteValue, RegistryValueKind.Binary);
return true;
}
catch (Exception) { /* ignore */ }
return false;
}
#endregion
#region Delete Registry Function
///
/// Registry KeyValue Delete Function
///
/// The Value to delete in the Registry
/// true if successful, false otherwise
public bool DeleteKey(string strKeyValue)
{
try
{
if (_RegKey != null)
{
_RegKey.DeleteValue(strKeyValue, false);
return true;
}
}
catch (Exception) { /* ignore */ }
return false;
}
#endregion
#region Public Static Helper Functions
///
/// Generic Static Registry Writer Function * Allows writing to HKEY\CURRENT_USER\SOFTWARE *
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// SubKey i.e. Microsoft, Microsoft\Windows
/// Value to write to
/// Value to write
/// true if successful, false otherwise
public static bool SetKey(String SubKey, String KeyValue, T Value)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(HKEYRoot.CurrentUser, KeySub.Software, SubKey, false))
{
bRetVal = reg.SetVal(KeyValue, Value);
}
return bRetVal;
}
///
/// Generic Static Registry Writer For Binary Function * Allows writing to HKEY\CURRENT_USER\SOFTWARE *
///
/// SubKey i.e. Microsoft, Microsoft\Windows
/// Value to write to
/// Binary Value to write
/// true if successful, false otherwise
public static bool SetKey(String SubKey, String KeyValue, byte[] ByteValue)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(HKEYRoot.CurrentUser, KeySub.Software, SubKey, false))
{
bRetVal = reg.SetVal(KeyValue, ByteValue);
}
return bRetVal;
}
///
/// Generic Static Registry Writer Function * Allows writing to any key *
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// RootKey as defined in this Class
/// SubKey i.e. Software, Software\Microsoft
/// Value to write to
/// Value to write
/// true if successful, false otherwise
public static bool SetKey(HKEYRoot rootKey, String SubKey, String KeyValue, T Value)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(rootKey, KeySub.Custom, SubKey, false))
{
bRetVal = reg.SetVal(KeyValue, Value);
}
return bRetVal;
}
///
/// Generic Static Registry Writer For Binary Function * Allows writing to any key *
///
/// RootKey as defined in this Class
/// SubKey i.e. Software, Software\Microsoft
/// Value to write to
/// Binary Value to write
/// true if successful, false otherwise
public static bool SetKey(HKEYRoot rootKey, String SubKey, String KeyValue, byte[] ByteValue)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(rootKey, KeySub.Custom, SubKey, false))
{
bRetVal = reg.SetVal(KeyValue, ByteValue);
}
return bRetVal;
}
///
/// Static Registry Delete Function * Allows Deleting from HKEY\CURRENT_USER\SOFTWARE *
///
/// SubKey i.e. Microsoft, Microsoft\Windows
/// Value to delete
/// true if successful, false otherwise
public static bool DeleteKey(String SubKey, String KeyValue)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(HKEYRoot.CurrentUser, KeySub.Software, SubKey, false))
{
bRetVal = reg.DeleteKey(KeyValue);
}
return bRetVal;
}
///
/// Static Registry Delete Function * Allows Deleting any key *
///
/// RootKey as defined in this Class
/// SubKey i.e. Software, Software\Microsoft
/// Value to delete
/// true if successful, false otherwise
public static bool DeleteKey(HKEYRoot rootKey, String SubKey, String KeyValue)
{
bool bRetVal = false;
using (RegKey reg = new RegKey(rootKey, KeySub.Custom, SubKey, false))
{
bRetVal = reg.DeleteKey(KeyValue);
}
return bRetVal;
}
///
/// Generic Static Registry Reader Function * Allows reading from HKEY\CURRENT_USER\SOFTWARE *
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// SubKey i.e. Microsoft, Microsoft\Windows
/// Value to write to
/// Default Value to return if none found or error occured
/// true if successful, false otherwise
public static T GetKey(String SubKey, String KeyValue, T DefaultValue)
{
T tRetVal = default(T);
using (RegKey reg = new RegKey(HKEYRoot.CurrentUser, KeySub.Software, SubKey, true))
{
tRetVal = reg.GetVal(KeyValue, DefaultValue);
}
return tRetVal;
}
///
/// Generic Static Registry Reader For Binary Function * Allows reading from HKEY\CURRENT_USER\SOFTWARE *
///
/// SubKey i.e. Microsoft, Microsoft\Windows
/// Value to write to
/// true if successful, false otherwise
public static Byte[] GetKey(String SubKey, String KeyValue)
{
Byte[] retByte = null;
using (RegKey reg = new RegKey(HKEYRoot.CurrentUser, KeySub.Software, SubKey, true))
{
retByte = reg.GetVal(KeyValue);
}
return retByte;
}
///
/// Generic Static Registry Reader Function * Allows reading from any key *
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// RootKey as defined in this Class
/// SubKey i.e. Software, Software\Microsoft
/// Value to write to
/// Default Value to return if none found or error occured
/// true if successful, false otherwise
public static T GetKey(HKEYRoot rootKey, String SubKey, String KeyValue, T DefaultValue)
{
T tRetVal = default(T);
using (RegKey reg = new RegKey(rootKey, KeySub.Custom, SubKey, true))
{
tRetVal = reg.GetVal(KeyValue, DefaultValue);
}
return tRetVal;
}
///
/// Generic Static Registry Reader For Binary Function * Allows reading from any key *
///
/// Should be a System Type like string, bool, int32, double, decimal, etc...
/// RootKey as defined in this Class
/// SubKey i.e. Software, Software\Microsoft
/// Value to write to
/// true if successful, false otherwise
public static Byte[] GetKey(HKEYRoot rootKey, String SubKey, String KeyValue)
{
Byte[] retByte = null;
using (RegKey reg = new RegKey(rootKey, KeySub.Custom, SubKey, true))
{
retByte = reg.GetVal(KeyValue);
}
return retByte;
}
#endregion
#region IDisposable Members
///
/// Dispose the Registry Handle
///
public void Dispose()
{
Dispose(true);
// Use SupressFinalize in case a subclass
// of this type implements a finalizer
GC.SuppressFinalize(this);
}
///
/// Dispose the Registry Handle
///
/// true, if called from within
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
#if NET4
if (disposing)
{
if (_RegKey != null)
_RegKey.Dispose(); // Works in .Net 4.0 Only
}
#endif
// Indicate that the instance has been disposed.
_RegKey = null;
_disposed = true;
}
}
#endregion
}
}