Share via


逐步解說:建立登錄機碼和變更其值

更新:2007 年 11 月

這個逐步解說會示範如何建立可以瀏覽至電腦登錄機碼的應用程式,讓使用者可以建立和刪除機碼,同時還會示範如何讀取、取得、設定和刪除機碼值。

若要建立主要表單

  1. 在 [檔案] 功能表中選取 [新增專案],然後按一下 [Windows 應用程式]。

  2. 在表單中加入名為 Value 的 TextBox。在右下角的 [屬性] 視窗中,於 [(名稱)] 欄位中輸入 Value。

  3. 在表單中加入名為 History 的 ListBox。在右下角的 [屬性] 視窗中,於 [(名稱)] 欄位中輸入 History。

  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. 將下列程式碼加入至 selectHive 的 SelectedIndexChanged 事件。

    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. 將含有 "Read Value" 文字且名為 ReadValueButton 的 Button 加入至表單。

  2. 將含有 "Enter Subkey" 文字且名為 showSubKey 的 TextBox 加入至表單。

  3. 將下列程式碼加入至 ReadValueButton 的 Click 事件:

    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. 將含有 "Set Value" 文字且名為 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. 藉由在 [Value] 文字方塊中輸入子機碼的新值,然後使用名為 ReadValueButton 的按鈕確認其值是否已變更,測試您的應用程式。

若要建立登錄機碼

  1. 將含有 "Create Key" 文字且名為 CreateButton 的按鈕加入至表單。

  2. 將下列程式碼加入至 Click 事件。

    registryObject.CreateSubKey(showSubKey.Text)
    History.Items.Add("Create Key " & selectHive.Text & _
                      "\" & showSubKey.Text)
    
  3. 藉由在 [showSubKey] 文字方塊中輸入新的機碼名稱,然後使用 [登錄編輯程式] 確認您的機碼是否已建立,測試您的應用程式。

若要刪除登錄機碼

  1. 將含有 "Delete Key" 文字且名為 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 命名空間讀取和寫入登錄