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:
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "MyTestKeyValue", "This is a test value.")
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"
My.Computer.Registry.LocalMachine.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\TestKey", "MyTestKeyValue", "This is a test value.")
From what I understand this is an issue with the application not having the correct permissions to read/write to the registry.
I've seen the following example out there but am unable to get it working on my machine:
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()
Lastly, I've also tried what is outlined in this very promising YouTube tutorial (https://www.youtube.com/watch?v=rrt9ti6bYi4), but again, no luck:
Module RegistryFunctions
Public Sub Read_Registry()
Try
Dim pregkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTracker", True)
Dim pRegKey_User = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTracker", 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: " & ex.Message)
End Try
End Sub
Public Sub Write_Registry()
Try
Dim Newkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTracker", True)
If Newkey Is Nothing Then Newkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\ScopeCreep\VideoTracker")
Newkey.SetValue("Server", frmLogin.tbServer.Text)
Newkey.SetValue("Enabled", frmLogin.cbEnabled.Checked)
Newkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\ScopeCreep\VideoTracker", True)
If Newkey Is Nothing Then Newkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\ScopeCreep\VideoTracker")
Newkey.SetValue("User", frmLogin.tbUser.Text)
Newkey.SetValue("Database", frmLogin.tbDatabase.Text)
Catch ex As Exception
MsgBox("Error in Write Registry: " & ex.Message)
End Try
End Sub
End Module
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.
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
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!
Thank you in advance!