【发布时间】:2018-07-07 14:38:16
【问题描述】:
我正在尝试打开一个注册表项,以便删除它的子项:
Dim asu As New System.Security.Principal.NTAccount(username.Text)
Dim si As System.Security.Principal.SecurityIdentifier = asu.Translate(GetType(System.Security.Principal.SecurityIdentifier))
Dim MyReg As Microsoft.Win32.RegistryKey
MyReg = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, host.Text) _
.OpenSubKey("Software\Microsoft\Windows NT\currentVersion\ProfileList\" & si.ToString & "\")
Dim myval As String
myval = MyReg.GetValue("Guid")
MsgBox(myval.ToString) ' retuns GUID no errors
Dim guid As String
guid = myval
Dim MyReg2 As Microsoft.Win32.RegistryKey
MyReg2 = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, host.Text) _
.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid\")
MsgBox(MyReg2.ToString)
'myreg2.DeleteSubKey(guid)
现在我已经测试了同一级别的其他键:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileListHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileNotification
它们都返回值,但在尝试打开 ProfileGuid 时会抛出 NullReferenceException。我可以完全访问远程注册表,并且我还在本地对其进行了测试,结果相同。我知道密钥存在。
有什么方法可以直接删除它而不打开子键?或者任何人都可以解释为什么它返回 null?
【问题讨论】:
-
ive 还尝试循环遍历顶层 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
codeDim SubKeyNames() As String = MyReg2.GetSubKeyNames() For Each s As String In SubKeyNames MsgBox (s.ToString)接下来code我还给了“每个人”对密钥的完全访问权限,但仍然没有返回任何内容
标签: vb.net visual-studio-2013 registry nullreferenceexception