0

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\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: " & 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: " & 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!

user3697824
  • 538
  • 4
  • 15
  • 1
    [When - and why - should you store data in the Windows Registry?](http://stackoverflow.com/q/268424/1070452) – Ňɏssa Pøngjǣrdenlarp Jun 04 '16 at 21:22
  • While I agree that you shouldn't use the registry for a new application, that doesn't really apply in this case (sorry I didn't mention that). Unfortunately, I am working with a legacy application that was meant for a system running Windows 98 or 2000. The registry keys that need to be changed are are located in HKLM. I'm essentially trying to give the user a way to change the registry using a GUI instead of running a batch file, PowerShell script or editing manually. Also, this is only a portion of what the application does. If I can import a .reg file, I would also accept that as a fix. – NoobieMcNooberson Jun 06 '16 at 01:20
  • While posting that last comment, I may have discovered that it might be easier than I think if I use the import option. I can use the following line to import my .reg file, I just need to figure out how to do it silently (other than the need for admin permissions (which I'm okay with). Process.Start(My.Computer.FileSystem.CurrentDirectory & "\test.reg") – NoobieMcNooberson Jun 06 '16 at 03:15

1 Answers1

1

By taking the approach of importing a .reg file, I was able to successfully change the registry keys under HKLM. I might add it was also WAY easier this way. There is still some tweaking that needs to be done, but this did the trick!

Hopefully this helps someone out who was in my same boat!

For more information, I located the answer here: http://www.vbforums.com/showthread.php?610140-Silently-import-reg-file

    Private Sub btnRegKeys_Click(sender As Object, e As EventArgs) Handles btnRegKeys.Click

    ' Dimensioning variables for the 64 bit keys, 32 bit keys and regedit
    Dim reg64 As String = (Application.StartupPath() & "\64bit.reg")
    Dim reg32 As String = (Application.StartupPath() & "\32bit.reg")
    Dim regedit As String = "regedit.exe"

    ' New ProcessStartInfo created
    Dim p As New ProcessStartInfo
    ' Specify the location of regedit
    p.FileName = regedit

    ' Checks the OS to see if it is x86 or x64 '
    If Environment.Is64BitOperatingSystem = True Then

        ' Displays if the system OS is x64 '
        MessageBox.Show("This is an x64 system.")

        ' Runs the 64 bit reg keys with the silent argument "/s"
        p.Arguments = "/s  """ & reg64

        ' Start the process
        Process.Start(p)

        ' Displays if the system OS is x86 '
    Else : MessageBox.Show("This system is an x86 system.")

        ' Runs the 32 bit reg keys with the silent argument "/s"
        p.Arguments = "/s  """ & reg32

        ' Start the process
        Process.Start(p)

    End If