Editing Registry Keys in VB.NET

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
2 posts Page 1 of 1
Contributors
User avatar
MrAlicard
VIP - Donator
VIP - Donator
Posts: 54
Joined: Thu Aug 05, 2010 4:08 pm

Editing Registry Keys in VB.NET
MrAlicard
Creating a Registry Key and Changing Its Values

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
1. Select New Project in the File menu and click Windows Application.
2. Add a TextBox named Value to the form. In the Properties window in the lower-right corner, in the (Name) field, type Value.
3. Add a ListBox named History to the form. In the Properties window in the lower-right corner, in the (Name) field, type History.
4. 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
1. 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.
Code: Select all
selectHive.Items.Add("ClassesRoot")
selectHive.Items.Add("CurentConfig")
selectHive.Items.Add("CurrentUser")
selectHive.Items.Add("LocalMachine")
selectHive.Items.Add("PerformanceData")
selectHive.Items.Add("Users")
2. Attach the following code after your class declaration.
Code: Select all
Dim registryObject As Microsoft.Win32.RegistryKey = Nothing
3. Add the following code to the selectHive
Code: Select all
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
1. Add to the form a Button named ReadValueButton with the text "Read Value".
2. Add to the form a TextBox named showSubKey with the text "Enter Subkey".
3. Add the following code to the ReadValueButton Click event.
Code: Select all
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)
4. 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
1. Add to the form a button named SetValueButton with the text "Set Value".
2. Add the following code to its Click event.
Code: Select all
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)
3. 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
1. Add to the form a button named CreateButton with the text "Create Key".
2. Add the following code to its Click event.
Code: Select all
registryObject.CreateSubKey(showSubKey.Text)
History.Items.Add("Create Key " & selectHive.Text & 
                  "\" & showSubKey.Text)
3. 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
1. Add a button to the form named DeleteButton with the text "Delete Key".
2. Add the following code to its Click event.
Code: Select all
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)
3. Test your code by deleting a subkey and using the Registry Editor to confirm that the key was deleted.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Editing Registry Keys in VB.NET
Usman55
Image
2 posts Page 1 of 1
Return to “Tutorials”