title: How to add/change registry keys in HKEY_LOCAL_MACHINE
body: <p>I've been struggling with adding keys and changing values in HKLM. I can add keys and set values in HKCU without issue using the following:</p>
  
  <pre><code>My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
  My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "MyTestKeyValue", "This is a test value.")
  </code></pre>
  
  <p>However, if I try something similar in using HKLM I receive the error "An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll"</p>
  
  <pre><code>My.Computer.Registry.LocalMachine.CreateSubKey("TestKey")
  My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\TestKey", "MyTestKeyValue", "This is a test value.")
  </code></pre>
  
  <p>From what I understand this is an issue with the application not having the correct permissions to read/write to the registry.</p>
  
  <p>I've seen the following example out there but am unable to get it working on my machine:</p>
  
  <pre><code>Dim autoshell = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Winlogon", True)
  '' Set the value to 0
  autoshell.SetValue("autorestartshell", 0)
  autoshell.Close()
  </code></pre>
  
  <p>Lastly, I've also tried what is outlined in this very promising YouTube tutorial (<a href="https://www.youtube.com/watch?v=rrt9ti6bYi4" rel="nofollow">https://www.youtube.com/watch?v=rrt9ti6bYi4</a>), but again, no luck:</p>
  
  <pre><code>    Module RegistryFunctions
      Public Sub Read_Registry()
          Try
  
              Dim pregkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTrac­ker", True)
              Dim pRegKey_User = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTrack­er", True)
  
              If Not (pRegKey_User Is Nothing) Then
                  frmLogin.tbUser.Text = pRegKey_User.GetValue("User")
                  frmLogin.tbDatabase.Text = pRegKey_User.GetValue("Database")
              End If
  
              If Not (pregkey Is Nothing) Then
                  frmLogin.tbServer.Text = pregkey.GetValue("Server")
                  frmLogin.cbEnabled.Checked = pregkey.GetValue("Enabled", False)
              End If
  
          Catch ex As Exception
              MsgBox("Error in function Read_Registry: " &amp; ex.Message)
          End Try
      End Sub
  
      Public Sub Write_Registry()
  
          Try
              Dim Newkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTrac­ker", True)
              If Newkey Is Nothing Then Newkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\ScopeCreep\VideoTr­acker")
  
              Newkey.SetValue("Server", frmLogin.tbServer.Text)
              Newkey.SetValue("Enabled", frmLogin.cbEnabled.Checked)
  
              Newkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTrack­er", True)
              If Newkey Is Nothing Then Newkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\ScopeCreep\VideoTra­cker")
  
              Newkey.SetValue("User", frmLogin.tbUser.Text)
              Newkey.SetValue("Database", frmLogin.tbDatabase.Text)
  
          Catch ex As Exception
              MsgBox("Error in Write Registry: " &amp; ex.Message)
  
          End Try
      End Sub
  End Module
  </code></pre>
  
  <p>The problem here, is that I'm not really sure how to link the buttons on the form to the code in the module. I tried the following (because I'm not really sure what to do) and it did not work.</p>
  
  <pre><code>    Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
      RegistryFunctions.Read_Registry()
  End Sub
  
  Private Sub btnWrite_Click(sender As Object, e As EventArgs) Handles btnWrite.Click
      RegistryFunctions.Write_Registry()
  End Sub
  </code></pre>
  
  <p>If anyone is able to fix something stupid that I did or provide any information on how I can complete this task, I would greatly appreciate it!</p>
  
  <p>Thank you in advance!</p>