.NET: Custom Registry Class

Important warning

​This article is not for people who never worked with registry editor. If you are trying to do something and you don't have experience with it, it is not my fault if your computer start to act weird.

​So if you want , you will do this exercise on your own risk.

Registry classes

There are 2 main registry classes in .NET which you have to know in order to work properly with registries.

​Those classes are:

  • Registry class
  • RegistryKey class

Both classes are part of Microsoft.Win32 namespace.

Learning

The best way to learn about them is by looking at this references:

What is the purpose of those classes?

The main purpose (at least for me) is to create small tools like uninstaller for annoying web browser toolbars which can be found in both Program Files\Program Files (x86) folder and in the registry. The bad thing is that if you can't find exact place where those registries are stored it will be very hard for you to find them because you have Local Machine, Current User, Classes Root, and others which stores a lot of registry values. 

After you figure out what Registry class and RegistryKey class can do you can look at this basic custom class (written by me) which helps you write a shorter code.

Custom class for registry (Visual C#.NET):

001.class RegistryCleaner : IRegistry
002.{
003.    //Class RegistryCleaner is inheriting IRegistry interface
004.    //provides methods for cleaning registry withing CurrentUser, ClassesRoot and LocalMachine
005.    //All methods within this class are boolean type because you can then write what you want in text controls when those
006.    //methods return true or false
007.    //
008.    //RegistrySubKeyTreeCU is method withing RegistryCleaner class to delete
009.    //sub key registries in CurrentUser registry
010.    public bool  RegistrySubKeyTreeCU(string a, string b)
011.    {
012.      //Creating instance of RegistryKey to take a path of sub key registry in CurrentUser
013.      Microsoft.Win32.RegistryKey RegKey1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(a, true);
014.      if (RegKey1 != null)
015.      {
016.        //if RegKey1 object is != null then code will delete the whole tree of the sub key
017.        RegKey1.DeleteSubKeyTree(b, true);
018.        //if deleting part is successful then method will return true
019.        return true;
020.      }
021.      //if RegKey1 object is equal to null then code will return false
022.      else return  false;
023.    }
024.    //
025.    //RegistrySubKeyTreeCR is method withing RegistryCleaner class to delete
026.    //sub key registries in ClassesRoot registry
027.    public bool  RegistrySubKeyTreeCR(string a, string b)
028.    {
029.      //Creating instance of RegistryKey to take a path of sub key registry in ClassesRoot
030.      Microsoft.Win32.RegistryKey RegKey2 = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(a, true);
031.      if (RegKey2 != null)
032.      {
033.        //if RegKey2 object is != null then code will delete the whole tree of the sub key
034.        RegKey2.DeleteSubKeyTree(b, true);
035.        //if deleting part is successful then method will return true
036.        return true;
037.      }
038.      //if RegKey2 object is equal to null then code will return false
039.      else return  false;
040.    }
041.    //
042.    //RegistrySubKeyTreeLM is method withing RegistryCleaner class to delete
043.    //sub key registries in LocalMachine registry
044.    public bool  RegistrySubKeyTreeLM(string a, string b)
045.    {
046.      //Creating instance of RegistryKey to take a path of sub key registry in LocalMachine
047.      Microsoft.Win32.RegistryKey RegKey3 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(a, true);
048.      if (RegKey3 != null)
049.      {
050.        //if RegKey3 object is != null then code will delete the whole tree of the sub key
051.        RegKey3.DeleteSubKeyTree(b, true);
052.        //if deleting part is successful then method will return true
053.        return true;
054.      }
055.      //if RegKey3 object is equal to null then code will return false
056.      else return  false;
057.    }
058.    //end of class
059.}
060. 
061.class PathCleaner : IPath
062.{
063.    //Class PathCleaner is inheriting IPath interface
064.    //Provide method for deleting specific path
065.    //Method within this class is boolean type becaue you can then write what you want in text controls when those
066.    //methods return true or false
067.    //
068.    //Path is a method which takes string as argument
069.    //That string MUST contain the path to specific a folder or a file
070.    public bool  Path(string  a)
071.    {
072.      //code will try to delete specific path of a folder or a file
073.      try
074.      {
075.        //code is deleting specific directory
076.        System.IO.Directory.Delete(a, true);
077.        //if everything is OK it will return true
078.        return true;
079.      }
080. 
081.      catch (Exception)
082.      {
083.        //if something goes wrong with deleting the directory it will return false
084.        return false;
085.      }
086.    }
087.    //end of class
088.}
089. 
090.  //Contract for Registry class
091.interface IRegistry
092.{
093.    bool RegistrySubKeyTreeCU(string a, string b);
094.    bool RegistrySubKeyTreeCR(string c, string d);
095.    bool RegistrySubKeyTreeLM(string e, string f);
096.}
097. 
098.  //Contract for Path class
099.interface IPath
100.{
101.    //Defining boolean method for deleting path
102.    bool Path(string a);
103.}

As said it is a small class and once you figure it out how it works, you can extend it. 

For an example I have created Babylon Toolbar Removal Tool. Don't be afraid, it is tested.

Babylon Toolbar Removal Tool

If you want to run it properly, run it as administrator. If you don't have that toolbar on your PC it will tell you. So this is the perfect opportunity to check if you have that toolbar installed.

If you have it and after you uninstall it with this small tool, go to your browser and check if it is still available for you to use that toolbar. If yes, just remove it and the entire process is completed.