我一直在为在HKLM中添加密钥和更改值而苦苦挣扎。我可以使用以下命令在HKCU中添加密钥和设置值,而不会出现问题:
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“
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\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
这里的问题是,我真的不确定如何将表单上的按钮链接到模块中的代码。我尝试了下面的方法(因为我真的不确定该怎么做),但没有起作用。
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
如果有人能够修复我做过的愚蠢的事情,或者提供任何关于我如何完成这项任务的信息,我将非常感激!
提前谢谢你!
发布于 2016-06-06 12:07:28
通过导入.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
https://stackoverflow.com/questions/37635066
复制相似问题