VB.NET • Manipulate the Registry

Listing 1. This module for interacting with the Registry simplifies Registry manipulation and makes saving and retrieving Registry entries as simple as making a function call within your program.

Module RegistryManipulation
Public HKEY_CLASSES_ROOT As String = _
   "\\HKEY_CLASSES_ROOT"
Public HKEY_CURRENT_USER As String = _
   "\\HKEY_CURRENT_USER"
Public HKEY_LOCAL_MACHINE As String = _
   "\\HKEY_LOCAL_MACHINE"
Public HKEY_USERS As String = _
   "\\HKEY_USERS"
Public HKEY_CURRENT_CONFIG As String = _
   "\\HKEY_CURRENT_CONFIG"
Public Function GetKey(ByVal KeyName _
   As String) As RegistryKey

   Dim rootKey As String = KeyName.Substring(0, _
      KeyName.IndexOf("\", 2))
   If Not (rootKey = HKEY_CLASSES_ROOT Or _
      rootKey = HKEY_CURRENT_USER Or _
      rootKey = HKEY_LOCAL_MACHINE Or _
      rootKey = HKEY_USERS Or _
      rootKey = HKEY_CURRENT_CONFIG) _
      Then
      Err.Raise(-1, _
         "Registry Manipulation", rootKey & _
         "is an invalid root key")
   End If
   Dim baseRegistry As RegistryKey
   Select Case rootKey
      Case HKEY_CLASSES_ROOT
         baseRegistry = Registry.ClassesRoot
      Case HKEY_CURRENT_USER
         baseRegistry = _
            My.Computer.Registry.CurrentUser
      Case HKEY_LOCAL_MACHINE
         baseRegistry = Registry.LocalMachine
      Case HKEY_USERS
         baseRegistry = Registry.Users
      Case HKEY_CURRENT_CONFIG
         baseRegistry = Registry.CurrentConfig
      Case Else
         baseRegistry = Registry.CurrentUser
   End Select

   Dim BaseKey As String = KeyName.Substring _
      (KeyName.IndexOf("\", 2) + 1)
   Dim retKey As RegistryKey
   retKey = baseRegistry.OpenSubKey (BaseKey, True)
   If retKey Is Nothing Then
      If BaseKey.IndexOf("\") = 0 Then 
         retKey = baseRegistry.CreateSubKey (BaseKey)
      Else
         Dim subBase As String = _
            BaseKey.Substring (0, BaseKey.LastIndexOf("\"))
         Dim keyOnly As String = _
            BaseKey.Substring(BaseKey.LastIndexOf("\") + 1)
         retKey = OpenKey(baseRegistry, _
            subBase).CreateSubKey(keyOnly)
      End If
   End If
   Return retKey
End Function

Public Function OpenKey(ByVal _
   BaseRegistry As RegistryKey, _
   ByVal KeyName As String) As _
   RegistryKey
   Dim retKey As RegistryKey
   retKey = BaseRegistry.OpenSubKey(KeyName, True)
   If retKey Is Nothing Then
      If KeyName.IndexOf("\") = 0 Then 
         retKey = BaseRegistry.CreateSubKey(KeyName)
      Else
   Dim subBase As String = KeyName.Substring(0, _
      KeyName.LastIndexOf("\"))
   Dim keyOnly As String = KeyName.Substring( _
      KeyName.LastIndexOf("\") + 1)
      retKey = OpenKey(BaseRegistry, _
         subBase).CreateSubKey(keyOnly)
      End If
   End If
   If retKey Is Nothing Then
      Err.Raise(-1, "Registry Manipulation", _
      "Could not open " & KeyName)
   End If
   Return retKey
End Function

Public Function SetRegistryValue( _
   ByVal KeyName As String, ByVal _
   EntryName As String, ByVal EntryValue _
   As Object) As Boolean
   Dim regKey As RegistryKey
   regKey = GetKey(KeyName)
   regKey.SetValue(EntryName, EntryValue)
   Return True
End Function
Public Function GetRegistryValue( _
   ByVal KeyName As String, ByVal _
   EntryName As String, ByVal _
   DefaultValue As Object) As Object
   Dim regKey As RegistryKey
   Dim retVal As Object
   regKey = GetKey(KeyName)
   retVal = regKey.GetValue(EntryName)
   If retVal Is Nothing Then
      retVal = DefaultValue
   End If
   Return retVal
End Function
End Module