Reliable way to test network connection

Sometimes you want to check if the network is up in your application, there is a built-in function for that : NetworkInterface.GetIsNetworkAvailable()

the problem with GetIsNetworkAvailable is that it incorrectly always report TRUE if a loopback adapter is present. It also report TRUE if a “VirtualBox Host-Only Network” adapter is present.

So I have written the following function in Visual Basic (vb) to replace GetIsNetworkAvailable :

Function IsNetworkAvailable() As Boolean
    IsNetworkAvailable = False
    Dim AllAdapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    Dim Adapter As NetworkInterface
    For Each Adapter In AllAdapters
        If Adapter.OperationalStatus = OperationalStatus.Up And
            Adapter.NetworkInterfaceType <> NetworkInterfaceType.Loopback And _
            Adapter.NetworkInterfaceType <> NetworkInterfaceType.Tunnel And _
            Not Adapter.Name.Contains("LoopBack") And _
            Not Adapter.Name.Contains("VirtualBox Host-Only Network") Then
            IsNetworkAvailable = True
        End If
    Next
End Function
Next PostNewer Post Previous PostOlder Post Home