演练:创建注册表项并更改其值

更新:2007 年 11 月

此演练演示如何创建应用程序来浏览计算机中的注册表项,以便用户可以创建和删除项,还演示如何读取、获取、设置和删除值。

创建主窗体

  1. 选择“文件”中的“新建项目”,然后单击“Windows 应用程序”。

  2. 将一个名为 Value 的 TextBox 添加到窗体中。在右下脚的“属性”窗口的“(名称)”字段中,键入“值”。

  3. 将一个名为 History 的 ListBox 添加到窗体中。在右下角的“属性”窗口的“(名称)”字段中,键入“历史记录”。

  4. 创建另一个变量并将其添加到紧随类声明之后的位置。

    Dim tempKey As Microsoft.Win32.RegistryKey
    

在组合框中浏览注册表项

  1. 将一个名为 selectHive 的 ComboBox 添加到窗体中,它将显示注册表单元并允许选择一个单元。通过将下面的代码添加到窗体的载入事件中填充该组合框。

    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. 在类声明之后附加下面的代码。

    Dim registryObject As Microsoft.Win32.RegistryKey = Nothing
    
  3. 将下面的代码添加到 selectHiveSelectedIndexChanged 事件中。

    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
    

读取注册表项的值

  1. 向窗体添加一个名为 ReadValueButton 且带有文本“读取值”的 Button

  2. 向窗体添加一个名为 showSubKey 且带有文本“输入子项”的 TextBox

  3. 将下面的代码添加到 ReadValueButtonClick 事件中。

    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. 通过在 showSubKey 文本框中输入一个现有子项的名称,测试应用程序。如果单击 ReadValueButton,Value 文本框将显示相应的值。

设置注册表项的值

  1. 向窗体添加一个名为 SetValueButton 且带有文本“设置值”的按钮。

  2. 将下面的代码添加到其 Click 事件中。

    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. 通过在“值”文本框中输入子项的新值,然后确认该值已由名为 ReadValueButton 的按钮更改,可以测试应用程序。

创建注册表项

  1. 向窗体添加一个名为 CreateButton 且带有文本“创建项”的按钮。

  2. 将下面的代码添加到其 Click 事件中。

    registryObject.CreateSubKey(showSubKey.Text)
    History.Items.Add("Create Key " & selectHive.Text & _
                      "\" & showSubKey.Text)
    
  3. 通过在“showSubKey”文本框中输入一个新项的名称并使用“注册表编辑器”确认已创建该项,可以测试应用程序。

删除注册表项

  1. 向窗体添加一个名为 DeleteButton 且带有文本“删除项”的按钮。

  2. 将下面的代码添加到其 Click 事件中。

    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. 通过删除一个子项然后使用“注册表编辑器”确认该项已删除,可以测试代码。

请参见

任务

疑难解答:操作注册表

概念

常见的注册表任务

安全性与注册表

使用 My 读取和写入注册表

使用 Microsoft.Win32 命名空间读取和写入注册表