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
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.
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.
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.
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.
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.
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
2. Attach the following code after your class declaration.
selectHive.Items.Add("ClassesRoot")
selectHive.Items.Add("CurentConfig")
selectHive.Items.Add("CurrentUser")
selectHive.Items.Add("LocalMachine")
selectHive.Items.Add("PerformanceData")
selectHive.Items.Add("Users")
Code: Select all
3. Add the following code to the selectHive
Dim registryObject As Microsoft.Win32.RegistryKey = Nothing
Code: Select all
To read a value in a registry keySelectedIndexChanged 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
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
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.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)
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
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.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)
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
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.registryObject.CreateSubKey(showSubKey.Text)
History.Items.Add("Create Key " & selectHive.Text &
"\" & showSubKey.Text)
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
3. Test your code by deleting a subkey and using the Registry Editor to confirm that the key was deleted.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)
2 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023