Windows Registry in VB.NET: Part 3
In this article I will explain you about the Windows Registry in VB.NET.
See Part 1, Part 2
If you want to delete a key, use the DeleteSubKey or the DeleteSubKeyTree methods. The RegistryKey.DeleteSubKey method deletes the specified subkey. The RegistryKey.DeleteSubKeyTree method deletes a subkey, its data, and all of its child subkeys recursively in one shot. If you want to delete a value, use the DeleteValue method of the RegistryKey class. The RegistryKey.DeleteValue method deletes the specified value from the key. These methods are illustrated in Listing 21.22.
Listing 21.22: Using DeleteValue and DeleteSubKey
' Deleting registry keys and values
' Delete the key value
Imports Microsoft.Win32
Class Class1
Shared Sub Main(ByVal args As String())
' Create a new key under HKEY_LOCAL_MACHINE\Software as MCBInc
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
' Add one more sub key
Dim newkey As RegistryKey = key.CreateSubKey("MCBInc")
' Set value of sub key
newkey.SetValue("MCBInc", "NET Developer")
' Retrieve data from other part of the registry
' find out your processor
Dim pRegKey As RegistryKey = Registry.LocalMachine
pRegKey = pRegKey.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0")
Dim val As [Object] = pRegKey.GetValue("VendorIdentifier")
Console.WriteLine("The central processor of this machine is:" & Convert.ToString(val))
' Delete the key value
Dim delKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software", True)
delKey.DeleteSubKey("MCBInc")
End Sub
End Class
There is another useful way to retrieve top-level registry handles (an alternative to the direct OpenSubKey method) for both local and remote computers. We can use the OpenRemoteBaseKey method:
Dim rgkey As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "MCBcomputer")
RegistryHive values are used by the OpenRemoteBaseKey method to represent the top-level node of a requested key on a foreign (remote) machine. The only nodes that can be opened with the OpenRemoteBaseKey method must be among the following top-level RegistryKeys. Further access to the subkeys of the identified node is available using methods in the RegistryKey class as long as the user has appropriate permission.
ClassesRoot: Represents the HKEY_CLASSES_ROOT base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
CurrentConfig: Represents the HKEY_CURRENT_CONFIG base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
CurrentUser: Represents the HKEY_CURRENT_USER base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
DynData: Represents the HKEY_DYN_DATA base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
LocalMachine: Represents the HKEY_LOCAL_MACHINE base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
PerformanceData: Represents the HKEY_PERFORMANCE_DATA base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
Users: Represents the HKEY_USERS base key on a foreign machine. This value can be passed to the OpenRemoteBaseKey method, to open this node remotely.
Listing 21.24 shows an example of reading some registry values on a foreign machine. Note that OpenRemoteBaseKey is opening the HKEY_LOCAL_MACHINE registry key on the machine named "ComputerNAME". Also note that OpenSubKey may not need a second Boolean parameter to indicate whether we are reading or writing to the registry because the single parameter call to OpenSubKey is read-only.
Listing 21.24: OpenRemoteBaseKey Illustrated (reg2.VB)
' Processor and Bios Information
Imports Microsoft.Win32
Public Class MyRegistry
Shared Sub Main()
Dim hklm As RegistryKey = Registry.LocalMachine
'or for a remote machine
' RegistryKey hklm = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,"ComputerNAME");
hklm = hklm.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0")
Dim obp As [Object] = hklm.GetValue("Identifier")
Console.WriteLine("Processor Identifier :{0}", obp)
hklm = Registry.LocalMachine
hklm = hklm.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0")
obp = hklm.GetValue("VendorIdentifier")
Console.WriteLine("Vendor Identifier :{0}", obp)
hklm = Registry.LocalMachine
hklm = hklm.OpenSubKey("HARDWARE\DESCRIPTION\System\MultiFunctionAdapter\4")
obp = hklm.GetValue("Identifier")
Console.WriteLine("Bios Status :{0}", obp)
hklm = Registry.LocalMachine
hklm = hklm.OpenSubKey("HARDWARE\DESCRIPTION\System\")
obp = hklm.GetValue("SystemBiosDate")
Console.WriteLine("Bios Date :{0}", obp)
hklm = Registry.LocalMachine
hklm = hklm.OpenSubKey("HARDWARE\DESCRIPTION\System\")
obp = hklm.GetValue("Identifier")
Console.WriteLine("System Identifer :{0}", obp)
End Sub
End Class
Output Window
Conclusion
Hope this article would have helped you in understanding the Windows Registry in VB.NET.