I tried to use the same code and also returned no value, although the key exists in Registry windows. I then tried this way:
Dim regKey = My.Computer.Registry.LocalMachine.OpenSubKey(
"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks")
Dim value = regKey.GetValue("NetworkList")
But he made a mistake in the second line, saying that the variable regKey
did not contain value (exception Object reference not set to an instance of an object
).
I then executed the Process Monitor, of Sysinternals, filtering only the activities of Registry and found that the program was actually trying to access this key (note the subkey Wow6432node that was added): HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks
.
My Windows is 64-bit, so I soon imagined that the program was running at 32-bit. To be sure, I put two Labels in Form to check two things, whether the operating system was 64-bit and whether 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 out here)
I confirmed then that the OS was 64-bit, but the application was not, despite the Target CPU of my project being marked as Anycpu. That’s when I noticed the option Prefer 32-bit, just below the option Target CPU, in Project properties > Compile, which was already checked when I created the project. I unchecked this option and then the application ran as 64-bit.
I tried again to access the key of the register and this time it was found, but then began to give the exception Requested registry access is not allowed
, because I was trying to access HKEY_LOCAL_MACHINE
with an ordinary user. I ran Visual Studio as an administrator and finally it worked.
The only thing that didn’t work was the display of the value returned in a MsgBox
, as you had done, because, as this value is of type REG_MULTI_SZ
, the type returned was String()
.
EDIT:
Then I found out I didn’t even have to create the function Is64BitApp()
, because, besides the property Environment.Is64BitOperatingSystem
that I used in the code, there is also the property Environment.Is64BitProcess
:
Environment.Is64bitprocess Property
https://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx
After this question the user jnmoura made another, which I also answered, which complements a little this, so I will leave here as reference:
Access 64-bit key in Windows Registry through 32-bit application
I did the procedures, I left the whole in 32bit did not work, I passed all to 64 bit, but when I fill the datagrid, is the value "System.String[]"
– jnmoura
As I said in the reply, the return value will be a String array, so you have to check the
Length
of the array and go through each item of the array, or join all items into a single string:String.Join("; ", ValorRegistro)
.– Pedro Gaspar
I did the Procedure worked on the way: "SOFTWARE Microsoft Windows NT Currentversion Networklist Profiles{5D1CD90A-7DD6-47AC-A40F-794E920B96DB}" But in this path not: "SOFTWARE Microsoft Windows NT Currentversion Networklist". My machine must be in trouble. Thank you for your help..
– jnmoura