Download Source Code: Manipulate the Registry with Visual Basic and the RegistryProxy Class
RegistryProxy Class
The RegistryProxy class, new in .NET 2.0, is a proxy class that can increase productivity when a developer is creating code to programmatically manipulate the registry. The RegistryProxy class provides properties and methods for manipulating the registry.
The proxy pattern is used by the Visual Basic My feature to increase developer productivity. A proxy class is an extremely thin class that forwards all calls it receives to an underlying object. By convention, proxy classes are always suffixed with Proxy. The Visual Basic My feature utilizes proxies when accessing the clipboard, file system, and registry to expose shared methods that otherwise wouldn't be visible in IntelliSense.
Some uses for the RegistryProxy class are to create registry sub keys, set registry key values, get registry key values, and to delete registry sub keys.
Sample Code
Private Sub createRegistryKeyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createRegistryKeyButton.Click
' Create a registry sub key to store a value for the current user.
My.Computer.Registry.CurrentUser.CreateSubKey("MySubKey")
End Sub
Private Sub setRegistryKeyValueButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setRegistryKeyValueButton.Click
' Set the value of the sub key giving it the name 'MySubKeyValue' and setting its data to 'This is a test value.'.
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\MySubKey", "MySubKeyValue", "This is a test value.")
End Sub
Private Sub getRegistryKeyValueButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getRegistryKeyValueButton.Click
If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\MySubKey", "MySubKeyValue", Nothing) IsNot Nothing Then
' Display the sub key's data.
MsgBox(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\MySubKey", "MySubKeyValue", Nothing).ToString())
Else
MsgBox("Sub key not found.")
End If
End Sub
Private Sub deleteRegistryKeyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteRegistryKeyButton.Click
' Delete the sub key.
My.Computer.Registry.CurrentUser.DeleteSubKey("MySubKey")
End Sub
Click the link above to download Visual Basic source code in a Visual Studio 2005 solution which demonstrates how to use the RegistryProxy class to manipulate the registry.
Mike McIntyre
http://www.getdotnetcode.com