KnowDotNet NetRefactor

Why Migrate - .NET Eases The Task of Accessing the Registry

Wrapper Class For Registry Objects

by Les Smith
Print this Article Discuss in Forums

Unlike VB6, the .NET framework provides base classes for accessing the registry.  This article provides a wrapper class for the .NET base classes.

Registy is the base class that is used to access the Windows Registry in NET.  This class is found in the Microsoft.Win32 Namespace.  In this article I will show you how simple it is to create and access keys and values anywhere in the registry.  In the following code, I intend to save an install date for a product in its own key in the LOCAL_MACHINE section of the registry.  The GetValue method of the wrapper class returns the existing value of the specified key, if it exists, otherwise it returns an empty string.  If the key does not exist, a call is made to create the new key, and then a call is made to set the value for the key.  

   Dim oReg As New CRegistry()
  
Dim sInstallDate As String

  
' If the install date does not exist insert it
   If oReg.GetValue("LM", "Software\Microsoft\MyProduct", "ID") = "" Then
      ' not installed
      oReg.CreateKey("LM", "Software\Microsoft\MyProduct")
      sInstallDate = EncryptDecrypt(Connect.Major.ToString & _
       Connect.Minor.ToString & _
        Format(Today, msDateFormat))

      oReg.CreateKey("LM", "Software\Microsoft\MyProduct")
      oReg.SetValue("LM", "Software\Microsoft\MyProduct", "ID", sInstallDate)
  
End If

The class, shown in Figure 1, is a simple wrapper class for accessing the base methods of the Registry class, written in Visual Basic .NET.  It allows the user to use a two character abbreviation of the top-level key to denote which major section of the registry is to be accessed.  You can see the CSharp version of the code by clicking
here.


Figure 1 - VB.NET Code for CRegistry Class.


Imports Microsoft.Win32

Public Class CRegistry

  
Const CU = "CU" 'Current_User
   Const LM = "LM" ' Local_Machine
   Const CR = "CR" ' classes_root
   Const UR = "UR" ' Users
   Const CC = "CC" ' current_config

   Public Function GetValue(ByVal Key As String, ByVal SubKey As String, ByVal Entry As String) As Object
      Dim reg As RegistryKey

      
Try
         Select Case Key
            
Case CU : reg = Registry.CurrentUser.OpenSubKey(SubKey)
            
Case LM : reg = Registry.LocalMachine.OpenSubKey(SubKey)
            
Case UR : reg = Registry.Users.OpenSubKey(SubKey)
            
Case CC : reg = Registry.CurrentConfig.OpenSubKey(SubKey)
            
Case CR : reg = Registry.Users.OpenSubKey(SubKey)
        
End Select
         Return reg.GetValue(Entry, 0).ToString
      
Catch ex As System.Exception
        
Return String.Empty
      
End Try
   End Function

   Public Function CreateKey(ByVal Key As String, _
      
ByVal SubKey As String) As Boolean
      Dim reg As RegistryKey

      
Try
         Select Case Key
            
Case CU : reg = Registry.CurrentUser.CreateSubKey(SubKey)
            
Case LM : reg = Registry.LocalMachine.CreateSubKey(SubKey)
            
Case UR : reg = Registry.Users.CreateSubKey(SubKey)
            
Case CC : reg = Registry.CurrentConfig.CreateSubKey(SubKey)
            
Case CR : reg = Registry.Users.CreateSubKey(SubKey)
        
End Select
         Return True
      Catch ex As System.Exception
        
Return False
      End Try
   End Function

   Public Function SetValue(ByVal Key As String, _
      
ByVal SubKey As String, ByVal Item As String, _
      
ByVal Value As Object) As Boolean
      Dim reg As RegistryKey

      
Try
         Select Case Key
            
Case CU : reg = Registry.CurrentUser.OpenSubKey(SubKey, True)
            
Case LM : reg = Registry.LocalMachine.OpenSubKey(SubKey, True)
            
Case UR : reg = Registry.Users.OpenSubKey(SubKey, True)
            
Case CC : reg = Registry.CurrentConfig.OpenSubKey(SubKey, True)
            
Case CR : reg = Registry.Users.OpenSubKey(SubKey, True)
        
End Select
         reg.SetValue(Item, Value)
         reg.Close()
        
Return True
      Catch ex As System.Exception
        
Return False
      End Try
   End Function
End
Class

Top of Page


Figure 2 - C# Code for CRegistry.



using System;
using Microsoft.Win32;
namespace CSTestAddin
{
  
/// <summary>
  /// Summary description for CRegistry.
  ///
  public class CRegistry
   {
      
const string CU = "CU"; // current user
      const string LM = "LM"; // local machine
      const string CR = "CR"; // classes root
      const string UR = "UR"; // users
      const string CC = "CC"; // current config
    
      
public CRegistry()
      {
        
//
        // TODO: Add constructor logic here
        //
     }
      
public object GetValue(string Key,string SubKey,string Entry)
      {
         RegistryKey reg;
        
try
         {
            
switch (Key)
            {
              
case CU:
                  reg =Registry.CurrentUser.OpenSubKey(SubKey);
                  
break;
              
case LM:
                  reg= Registry.LocalMachine.OpenSubKey(SubKey);
                  
break;
              
case UR:
                  reg = Registry.Users.OpenSubKey(SubKey);
                  
break;
              
case CC:
                  reg = Registry.Users.OpenSubKey(SubKey);
                  
break;
              
case CR:
                  reg = Registry.Users.OpenSubKey(SubKey);
                  
break;
            }
            
return reg.GetValue(Entry,0).ToString;
         }
        
catch
         {
            
return false;
         }
      }
      
public bool CreateKey(string Key,string SubKey)
         {
            RegistryKey reg;
            
try
            {
              
switch (Key)
               {
                  
case CU:
                     reg =Registry.CurrentUser.CreateSubKey(SubKey);
                    
break;
                  
case LM:
                     reg= Registry.LocalMachine.CreateSubKey(SubKey);
                    
break;
                  
case UR:
                     reg = Registry.Users.CreateSubKey(SubKey);
                    
break;
                  
case CC:
                     reg = Registry.Users.CreateSubKey(SubKey);
                    
break;
                  
case CR:
                     reg = Registry.Users.CreateSubKey(SubKey);
                    
break;
               }
              
return true;
            }
            
catch
            {
              
return false;
            }

         }
      
public bool SetValue(string Key,string SubKey,
        
string Item,object Value)
      {
         RegistryKey reg;
        
try
         {
            
switch (Key)
            {
              
case CU:
                  reg =Registry.CurrentUser.OpenSubKey(SubKey,
true);
                  
break;
              
case LM:
                  reg= Registry.LocalMachine.OpenSubKey(SubKey,
true);
                  
break;
              
case UR:
                  reg = Registry.Users.OpenSubKey(SubKey,
true);
                  
break;
              
case CC:
                  reg = Registry.Users.OpenSubKey(SubKey,
true);
                  
break;
              
case CR:
                  reg = Registry.Users.OpenSubKey(SubKey,
true);
                  
break;
            }
            reg.SetValue(Item,Value);
            reg.Close;
            
return true;
         }
        
catch
         {
            
return false;
         }
      }
   }
}

Top of Page

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing