首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在HKEY_LOCAL_MACHINE中添加/更改注册表项

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

Stack Overflow用户
提问于 2016-06-05 05:11:20
回答 1查看 3.6K关注 0票数 0

我一直在为在HKLM中添加密钥和更改值而苦苦挣扎。我可以使用以下命令在HKCU中添加密钥和设置值,而不会出现问题:

代码语言:javascript
运行
复制
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\TestKey", "MyTestKeyValue", "This is a test value.")

但是,如果我在使用HKLM时尝试类似的操作,我会收到错误消息"An unhandled exception of type 'System.IO.IOException‘occurred in mscorlib.dll“

代码语言:javascript
运行
复制
My.Computer.Registry.LocalMachine.CreateSubKey("TestKey")
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\TestKey", "MyTestKeyValue", "This is a test value.")

据我所知,这是应用程序没有正确的注册表读/写权限的问题。

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

代码语言:javascript
运行
复制
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)中概述的内容,但还是没有成功:

代码语言:javascript
运行
复制
    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

这里的问题是,我真的不确定如何将表单上的按钮链接到模块中的代码。我尝试了下面的方法(因为我真的不确定该怎么做),但没有起作用。

代码语言:javascript
运行
复制
    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

如果有人能够修复我做过的愚蠢的事情,或者提供任何关于我如何完成这项任务的信息,我将非常感激!

提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2016-06-06 12:07:28

通过导入.reg文件的方法,我能够成功地更改HKLM下的注册表项。我可能会补充说,这样做也更容易。仍然有一些需要调整的地方,但这已经奏效了!

希望这能帮助和我有同样遭遇的人!

有关更多信息,我在这里找到了答案:http://www.vbforums.com/showthread.php?610140-Silently-import-reg-file

代码语言:javascript
运行
复制
    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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37635066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档