Walkthrough: Creating a Registry Key and Changing Its Values (Visual Basic)
This walkthrough demonstrates how to create an application that will browse to registry keys on the computer so users can create and delete keys, as well as how to read, get, set, and delete values.
To create the main form
Select New Project in the File menu and click Windows Application.
Add a TextBox named Value to the form. In the Properties window in the lower-right corner, in the (Name) field, type Value.
Add a ListBox named History to the form. In the Properties window in the lower-right corner, in the (Name) field, type History.
Create the additional variable and add it immediately after the class declaration.
Dim tempKey As Microsoft.Win32.RegistryKey
To browse registry keys in a ComboBox
Add to your form a ComboBox named selectHive, which will display the registry hives and allow you to select one. Populate it by adding the following code to the form's load event.
selectHive.Items.Add("ClassesRoot") selectHive.Items.Add("CurentConfig") selectHive.Items.Add("CurrentUser") selectHive.Items.Add("LocalMachine") selectHive.Items.Add("PerformanceData") selectHive.Items.Add("Users")
Attach the following code after your class declaration.
Dim registryObject As Microsoft.Win32.RegistryKey = Nothing
Add the following code to the selectHive SelectedIndexChanged event.
Select Case selectHive.Text Case "ClassesRoot" registryObject = My.Computer.Registry.ClassesRoot Case "CurrentConfig" registryObject = My.Computer.Registry.CurrentConfig Case "CurrentUser" registryObject = My.Computer.Registry.CurrentUser Case "LocalMachine" registryObject = My.Computer.Registry.LocalMachine Case "PerformanceData" registryObject = My.Computer.Registry.PerformanceData Case "Users" registryObject = My.Computer.Registry.Users End Select
To read a value in a registry key
Add to the form a Button named ReadValueButton with the text "Read Value".
Add to the form a TextBox named showSubKey with the text "Enter Subkey".
Add the following code to the ReadValueButton Click event.
tempKey = registryObject If tempKey Is Nothing Then MsgBox("Please select a registry hive.") Return End If Value.Text = CStr(tempKey.GetValue(ShowSubKey.Text)) History.Items.Add("Read Value " & selectHive.Text & "\" & ShowSubKey.Text)
Test your application by entering the name of an existing subkey into the showSubKey textbox. When the ReadValueButton is clicked, the Value text box displays the value.
To set a value in a registry key
Add to the form a button named SetValueButton with the text "Set Value".
Add the following code to its Click event.
tempKey = registryObject If tempKey Is Nothing Then MsgBox("Please select a registry hive.") Return End If If Value.Text Is Nothing Then MsgBox("Please enter a value.") Return End If tempKey.SetValue(showSubKey.Text, Value.Text) tempKey.Close() History.Items.Add("Set Value " & selectHive.Text & "\" & showSubKey.Text)
Test your application by entering a new value for a subkey in the Value text box and then confirming that the value has been changed with the button named ReadValueButton.
To create a registry key
Add to the form a button named CreateButton with the text "Create Key".
Add the following code to its Click event.
registryObject.CreateSubKey(showSubKey.Text) History.Items.Add("Create Key " & selectHive.Text & "\" & showSubKey.Text)
Test your application by entering a new key name in the showSubKey text box and using the Registry Editor to confirm that your key has been created.
To delete a registry key
Add a button to the form named DeleteButton with the text "Delete Key".
Add the following code to its Click event.
tempKey = registryObject If tempKey Is Nothing Then MsgBox("Please select a registry hive.") Return End If If showSubKey.Text Is Nothing Then MsgBox("Please enter a subkey.") Return End If registryObject.DeleteSubKey(showSubKey.Text) History.Items.Add("Delete Key " & selectHive.Text & "\" & showSubKey.Text)
Test your code by deleting a subkey and using the Registry Editor to confirm that the key was deleted.
See Also
Tasks
Troubleshooting: Manipulating the Registry (Visual Basic)
Concepts
Common Registry Tasks (Visual Basic)
Security and the Registry (Visual Basic)
Reading from and Writing to the Registry Using My (Visual Basic)
Reading from and Writing to the Registry Using the Microsoft.Win32 Namespace (Visual Basic)