I'm trying to open a registry key so I can delete its subkeys:
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)
Now I have tested other keys on the same level:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileListHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileNotification
They all return values, but when trying to open ProfileGuid it throws a NullReferenceException. I have full access to the remote registry and I've also tested it locally with the same results. I know the key exists.
Is they any way I can delete it directly without opening subkeys? Or can anyone explain why it is returning null?
You're most likely experiencing what's called Registry Redirection.
To maintain compatibility with 32-bit applications, 64-bit versions of Windows implemented the File System Redirector and the Registry Redirector. The purpose of these two is to keep a seperate set of files and registry keys (usually goes under the name WOW64) that are specific to 32-bit applications only.
For instance, due to that a 64-bit process cannot load 32-bit code, a separate system directory is kept with only 32-bit versions of the system DLLs and applications. The path to the 32-bit system directory is %SystemRoot%\SysWOW64, while the 64-bit directory is the standard %SystemRoot%\System32.
This works the same way in the registry, though only a certain set of keys have a respective 32-bit key. The 32-bit key is always located as a subkey of the standard 64-bit key (for example HKLM\SOFTWARE) and is called Wow6432Node.
The File System Redirector (and respectively the Registry Redirector) automatically redirects all 32-bit apps to the respective 32-bit directory/registry key to ensure that 32-bit apps still work on 64-bit systems. By default Visual Studio projects target 32-bit only.
As far as I know there are two ways to overcome this problem:
Compile your app as
AnyCPUinstead ofx86.This is by far the most simple solution. By doing this the app will automatically run as a 32-bit app on a 32-bit system, or as a 64-bit app on a 64-bit system. Thus the Registry Redirector won't need to intervene.
Specify if you want to access the 32-bit view or the 64-bit view of the registry.
The .NET Framework has a built in feature that lets you specify if you want to access the 32-bit view or the 64-bit view of the registry. Combine that with
Environment.Is64BitOperatingSystemto determine which view you want to access.Local solution (for others seeing this):
'Determine which registry view to use. Dim RegView As RegistryView = If(Environment.Is64BitOperatingSystem, RegistryView.Registry64, RegistryView.Registry32) 'Opens HKEY_LOCAL_MACHINE with the specified registry view. Using RegHive As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegView) 'Open the "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid" key. Using RegKey As RegistryKey = RegHive.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid", True) 'Do stuff with the registry key... End Using End UsingRemote solution:
(same as above, but changed the
Using RegHive As RegistryKeyline)Using RegHive As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, host.txt, RegView)
来源:https://stackoverflow.com/questions/48492135/nullreferenceexception-when-opening-a-subkey-i-know-exists