How do we check to see that a specific network is available, in Visual Basic .NET?
Solution
To see if any network is available you can use the VB.Net My namespace:
My.Computer.Network.IsAvailable
To see if you can use an available network to get to a specific server, you can use:
My.Computer.Network.Ping(host name or IP address, or a System.Uri)
DNS Test
Another solution was to test with DNS. This way you can test for specific names inside xxx network to tell if you are inside/outside. The nested try statements shows this concept:
Imports System.Net Module Networker Dim Online_Status As Boolean = vbFalse Dim InsideJoeNetwork As Boolean = vbFalse Dim CurrentJoeIPAddress As New IPHostEntry Public ReadOnly Property GetOnlineStatus() As String Get Return Online_Status End Get End Property Public ReadOnly Property InsideJoeNet() As String Get Return InsideJoeNetwork End Get End Property Sub Initialize() Set_Online_Status() End Sub Public Sub Set_Online_Status() If My.Computer.Network.IsAvailable Then Try Dim DNSTest As IPHostEntry = Dns.GetHostEntry("google.com") If DNSTest.AddressList.Length > 0 Then Online_Status = True Detect_Joe_Network() Else : Online_Status = False End If Catch ex As System.Net.Sockets.SocketException Online_Status = False End Try End If End Sub Public Sub Detect_Joe_Network() If Online_Status = True Then Dim JoeIP As IPHostEntry = New IPHostEntry() Try JoeIP = Dns.GetHostEntry("laptop") If JoeIP.AddressList.Length > 0 Then InsideJoeNetwork = True CurrentJoeIPAddress = JoeIP 'MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information) End If Catch ex As Sockets.SocketException Try JoeIP = Dns.GetHostEntry("laptop.exampledomain.com") If JoeIP.AddressList.Length > 0 Then InsideJoeNetwork = False CurrentJoeIPAddress = JoeIP ' MessageBox.Show(JoeIP.HostName.ToString(), "JoeIP", MessageBoxButtons.OK, MessageBoxIcon.Information) End If Catch ey As Sockets.SocketException End Try End Try End If End Sub End Module