RegistryPlus - Advanced Registry Manipulation with .NET

While I was working on my Remote Desktop Assistant tool, I was getting really frustrated with the Registry. I couldn't bind registry values to a WinForm control, and it was a pain to manipulate when I needed to change a dozen or more entries. I also needed to access values by collection index, and a syntax like RegistryKey.Items("UserName").Value = "SomeUsername" would have been nice too. So I created a really small and simple API to solve those problems, and it's called RegistryPlus.

Basically, it has 3 objects:

  • SuperRegistryKey - a more advanced version of Microsoft.Win32.RegistryKey.
  • RegistryEntry - A strongly-typed object that stores an entry name and a value.
  • RegistryEntryCollection - A collection of RegistryEntries that you can DataBind against.

It is really straightforward to interact with. Here's an example that binds the key values agsinst a CheckedListBox.

1Public Sub Form_Load(ByVal sender As Object, ByVal e as EventArgs) Handles MyBase.Load 
2 Dim key As New SuperRegistryKey("Software\Microsoft\Terminal Server Client\Default", True)
3 CheckedListBox1.DisplayMember = "Value"
4 CheckedListBox1.DataSource = key.Entries
5End Sub

In that code, we've opened up the key specified in the constructor, made sure we could write to it. If you look at Line 4, we're binding to the "Entries" property, which contains a RegistryEntryCollection populated with values. And in Line 3, we're displaying the "Value" property of the DirectoryEntry object.

So now lets say that you want to add some values to the key. Well, the code for that is really simple.

1Public Sub Button1_Click(ByVal sender As Object, ByVal e as EventArgs) Handles Button1.Click 
2 Dim key As New SuperRegistryKey("Software\Microsoft\Terminal Server Client\Default", True)
3 key.Entries.Add(New RegistryEntry("Username", "Robert")
4 key.Save()
5End Sub

Or if you want to access a specific entry's value:

1Public Sub Button1_Click(ByVal sender As Object, ByVal e as EventArgs) Handles Button1.Click 
2 Dim key As New SuperRegistryKey("Software\Microsoft\Terminal Server Client\Default", True)
3 MsgBox(key.Entries("Username").Value)
4 'Or you can do this:
5 For i As Integer = 0 To key.Entries.Count - 1
6 MsgBox(key.Entries(i).Value)
7 Next
8 'Or even this:
9 For Each entry As DirectoryEntry in key.Entries
10 MsgBox(entry.Value)
11 Next
12End Sub

There are many ways you can use it. And if you still want to interact with the key the old fashioned way, SuperRegistryKey has a property called RegistryKey that contains the Microsoft.Win32.RegistryKey instance that the SuperKey interacts with.

If you want to use it in your own code for free, you can download it here. It comes with the source, all I ask is that if you improve it, you send me the improvements so I can add them to the package. Hope this is useful to someone.

No Comments