I tried to use the same code and also did not return any value, although the key exists in the Registry of Windows. I tried it this way:
Dim regKey = My.Computer.Registry.LocalMachine.OpenSubKey(
"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks")
Dim value = regKey.GetValue("NetworkList")
But it gave error in the second line, saying that the variable regKey
contained no value (exception Object reference not set to an instance of an object
).
I then ran Sysinternals' Process Monitor , filtering only Registry and found that the program was actually trying to access this key (note the subkey that was added): HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks
.
My Windows is 64-bit, so I soon figured the program was running in 32-bit. To be sure, I put two labels on the Form to check for two things, if the operating system was 64-bit and if the application was 64-bit:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Is64BitOsLabel.Text = $"Is 64-bit OS: {Environment.Is64BitOperatingSystem}"
Is64BitAppLabel.Text = $"Is 64-bit App: {Is64BitApp()}"
End Sub
Private Function Is64BitApp() As Boolean
Return IntPtr.Size = 8
End Function
(The code for the function Is64BitApp()
I found here )
I then confirmed that the OS was 64-bit, but the application was not, although the Target CPU of my project was marked as AnyCPU . It was then that I noticed the Prefer 32-bit option, just below the Target CPU option in Project properties > Compile , which was already checked when I created the project. I unchecked this option and then the application started to run as 64-bit.
I tried again to access the registry key and this time it was found, but then it started to give the Requested registry access is not allowed
exception, because I was trying to access HKEY_LOCAL_MACHINE
with an ordinary user. I ran Visual Studio as an administrator and it finally worked.
The only thing that did not work was the display of the returned value in a MsgBox
, as you did, because since this value is of type REG_MULTI_SZ
, the returned type was String()
.
EDIT:
Then I discovered that I would not even need to have created the Is64BitApp()
function because, in addition to the Environment.Is64BitOperatingSystem
property I used in the code, there is also the Environment.Is64BitProcess
property:
Environment.Is64BitProcess Property
link
After this question the user jnmoura did another, which I also answered, which complements a bit this, so I will leave here as a reference:
Access 64-bit key in Windows Registry through 32-bit application