Walkthrough: 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.

    selectHive.Items.Add("ClassesRoot")
    selectHive.Items.Add("CurentConfig")
    selectHive.Items.Add("CurrentUser")
    selectHive.Items.Add("DynData")
    selectHive.Items.Add("LocalMachine")
    selectHive.Items.Add("PerformanceData")
    selectHive.Items.Add("Users")
    
    
    
  2. Attach the following code after your class declaration.

    Dim registryObject As Microsoft.Win32.RegistryKey = Nothing
    
    
    
  3. 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 "DynData"
        registryObject = My.Computer.Registry.DynData
      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.

    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.

    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.

    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.

    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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft