I need to get information about the network to list in an application I'm doing, but I'm having a problem accessing Windows Registry because of project preference.
Path: Right-click on the class, click properties, and then compiler.
Note that under the Target CPU field, you have a CheckBox (32-bit Prefer)
If this option is ticked my code does not work on 64 bit system, however if I disable my application it does not work on the 32 bit machine. If I comment on all procedures that look for values in regedit, the application works on both.
I wonder if I can access the registry key in either 64bit or 32bit.
Follow the code:
Public Sub ObterTipoDaRede(ByVal NomeConfiguracao As String, ByVal _
NomeConfiguracaoDois As String)
Dim NomeDaRede As String
Dim regKey
Dim lDatas As New List(Of KeyValuePair(Of String, DateTime))
'If Environment.Is64BitOperatingSystem Then
'Else
regKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles")
'End If
Try
For Each SubK In regKey.GetSubKeyNames
Dim value = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\" + SubK)
Dim data = My.Computer.Registry.GetValue(value.ToString, "DateLastConnected", Nothing)
Dim dt As DateTime = ObterDataDoUltimoAcesso(data)
lDatas.Add(New KeyValuePair(Of String, Date)(SubK, dt))
Next
lDatas = lDatas.OrderByDescending(Function(x) x.Value).ToList
Dim t = lDatas.First
Dim chave = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\" + t.Key)
Dim nomeRede As String = My.Computer.Registry.GetValue(chave.ToString, "ProfileName", Nothing)
Dim tipoRede As String = My.Computer.Registry.GetValue(chave.ToString, "Category", Nothing)
NomeDaRede = nomeRede
Select Case tipoRede
Case 0
Me.PerfilDaRede = "Publica"
Case 1
Me.PerfilDaRede = "Privada"
Case 2
Me.PerfilDaRede = "Dominio"
End Select
Me.PreencherGriddgvDadosComputador(NomeConfiguracao, NomeDaRede, "Ok")
Me.PreencherGriddgvDadosComputador(NomeConfiguracaoDois, Me.PerfilDaRede, "Ok")
Catch ex As Exception
MsgBox("Teste", vbInformation)
End Try
End Sub