How to read a value from a Registry key

Asked

Viewed 462 times

2

I’m trying to get a value from the registry key but the value returns empty, someone has another way to get the value from the registry key?

Follow the code in VB.NET

Private Sub ObterNomeDeRede()

Dim ValorRegistro = 
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows 
NT\CurrentVersion\NetworkList\NewNetWorks", "NetworkList", Nothing)

MsgBox(ValorRegistro, vbInformation)

End Sub

2 answers

2


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[]"

  • 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).

  • 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..

1

I left it that way:

 Private Sub ObterTipoDaRede()
    Dim pRegKey As RegistryKey = Registry.LocalMachine
    pRegKey = pRegKey.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks")
    Dim val As Object = pRegKey.GetValue("NetworkList")
    Me.teste = val
End Sub


Public Sub Preencher_Grid_dgvDadosComputador(Grid As Object)
    With Grid
        .Rows.Add("Nome_Do_Computador ", Me.NomeMaquina)
        .Rows.Add("Nome_Domínio ", Me.DominioMaquina)
        .Rows.Add("Pasta_(Moura_)", Me.PastaMoura)
        .Rows.Add("Firewall_Redes/Domínio", Me.FirewallRedesDominio)
        .Rows.Add("Firewall_Redes/Privadas", Me.FirewallRedesPrivadas)
        .Rows.Add("Firewall_Redes/Publicas", Me.FireWallRedesPublicasConvidadas)
        .Rows.Add("teste", Me.teste)
    End With
End Sub

In the Builder I call the sub

Public Sub New()

    ObterTipoDaRede()

End Sub

And in the form load I call the procedure of filling out the datagrid

 Dim Analise As New Analisar()


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Analise.Preencher_Grid_dgvDadosComputador(dgvDadosComputador)

End Sub
  • In the proceeding ObterTipoDaRede() you can change the last line to: Me.teste = String.Join("; ", val), thus joins the list of strings into a single string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.