如何在HKEY_LOCAL_MACHINE中添加/更改注册表项

时间:2016-06-04 21:11:20

标签: vb.net permissions registry

我一直在努力在HKLM中添加密钥和更改值。我可以使用以下内容在HKCU中添加密钥和设置值:

My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "MyTestKeyValue", "This is a test value.")

但是,如果我在使用HKLM时尝试类似的东西,我会收到错误"未处理的类型' System.IO.IOException'发生在mscorlib.dll"

My.Computer.Registry.LocalMachine.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\TestKey", "MyTestKeyValue", "This is a test value.")

根据我的理解,这是一个应用程序没有正确的读/写权限的问题。

我已经看到了以下示例,但我无法在我的机器上运行它:

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()

最后,我还尝试了这个非常有前途的YouTube教程(https://www.youtube.com/watch?v=rrt9ti6bYi4)中概述的内容,但同样,没有运气:

    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

这里的问题是,我不确定如何将表单上的按钮链接到模块中的代码。我尝试了以下内容(因为我不确定该怎么做)并且它不起作用。

    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

如果有人能够修复我做过的愚蠢或提供有关如何完成此任务的任何信息,我将不胜感激!

提前谢谢!

1 个答案:

答案 0 :(得分:1)

通过导入.reg文件的方法,我能够成功更改HKLM下的注册表项。我可能会添加它也更方便这种方式。还有一些需要做的调整,但这就行了!

希望这可以帮助那些在同一条船上的人!

有关详细信息,请在此处找到答案: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