Windows Registry in VB.NET: Part 2

In this article I will explain you about the Windows Registry in VB.NET.
  • 3852

See Part 1
The RegistryKey class enables you to manipulate data in a registry key; it contains members to add, remove, replace, and read registry data. Some of its common methods and properties are defined in Table 21.9.

Table 21.9: Registry Class

table21.9.gif

Let's see how to use these methods to add, remove, and update keys and their values. First we will add a subkey called HKEY_LOCAL_MACHINE/Software/MCBInc and place a value entry and a value inside (see Listing 21.18).

Let's perform some actions against the registry. We can use CreateSubKey to add a new key to the Registry and call the SetValue method to write a value and key. The code in Listing 21.18 does this for us. The second parameter of the OpenSubKey method is a Boolean that identifies whether our access is for reading or writing. Use false in the second parameter for reading a value, and use true for writing a value. That way you can prevent unplanned, unwanted write operations. The public void SetValue (string name, object value) function sets the specified value to the registry key. The key must be opened for write access, and the key name is not case sensitive. Values allowed in the registry must be of the type DWORD, binary, or string. You pass these object types as the second parameter to the SetValue method, which accepts Object as the parameter type. You should format your string values appropriately before setting their values. String values can be represented in the following categories in the registry:

  • SZ. Data is represented as a null-terminated Unicode string value.

  • MULTI_SZ. Data is represented as an array of null-terminated Unicode strings.

  • EXPANDED_SZ. Data is represented as a null-terminated Unicode string with expanded references to environment variables.

Since many values can be stored in each key in the registry, the name parameter specifies the particular value you wish to manipulate. To set the default value for a particular registry key, the name can be set to either a null reference or an empty string (""). Notice in Listing 21.18 that the second parameter to OpenSubKey is set to true to enable the registry writing operation on that key.

Listing 21.18: Using CreateSubKey and SetValue

Imports
Microsoft.Win32
Namespace TheWindowsRegistryCSharp
    Class Program
        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")
        End Sub
    End Class
End Namespace

The GetValue method returns the value of a subkey in the form of an object. In the example in Listing 21.19, we read the value of the CenteralProcessor\0 subkey and write it to the console. To get the default value for a particular registry key, set the name of the value in GetValue to either a null reference or an empty string ("").

Listing 21.19: Using GetValue

Imports
Microsoft.Win32
Namespace TheWindowsRegistryCSharp
    Class Program
        Shared Sub Main(ByVal args As String())
            ' 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))
            Console.ReadLine()
        End Sub
    End Class
End Namespace

You can also loop through all the subkeys inside a registry key by getting a collection of the subkeys as shown in Listing 21.20.

Listing 21.20: Using GetSubkeyNames

Imports
Microsoft.Win32 
Namespace TheWindowsRegistryCSharp
    Class Program
        Shared Sub Main(ByVal args As String())
            Dim regkey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Mindcracker", False
            For Each s As [String] In regkey.GetSubKeyNames()
                Console.WriteLine(s)
            Next
        End Sub
    End Class
End Namespace

Furthermore, you can loop through all the values of the registry key by getting a collection of all the values in the key, as Listing 21.21 demonstrates.

Listing 21.21: Using GetValueNames

            
            'retrieve the array of values for that key
            Dim sarray As [String]() = regkey.GetValueNames() 
            'write the values to the screen
            For Each s As [String] In sarray
                Console.WriteLine(s)
            Next

Conclusion

Hope this article would have helped you in understanding the Windows Registry in VB.NET. The third part of this article you will see in my next article.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.