This topic has not yet been rated - Rate this topic

NetworkInterface.GetIsNetworkAvailable Method

April 12, 2014

Indicates whether any network connection is available.

Namespace:  System.Net.NetworkInformation
Assembly:  System.Net (in System.Net.dll)
'Declaration
Public Shared Function GetIsNetworkAvailable As Boolean

Return Value

Type: System.Boolean
true if a network connection is available; otherwise, false.

A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.

There are many cases in which a device or computer is not connected to a useful network but is still considered available and GetIsNetworkAvailable will return true. For example, if the device running the application is connected to a wireless network that requires a proxy, but the proxy is not set, GetIsNetworkAvailable will return true. Another example of when GetIsNetworkAvailable will return true is if the application is running on a computer that is connected to a hub or router where the hub or router has lost the upstream connection.

The following example uses the GetIsNetworkAvailable method to determine if a network connection is available.


Public class Example

    Dim Shared online As Boolean = False

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        ' Initialize the online variable and set a NetworkChange handler
        SetupNetworkChange()

        outputBlock.Text &= "Network is: "
        If online Then
            outputBlock.Text &= "online"
        Else   
            outputBlock.Text &= "offline"
        End If    

        ' Now start the main work of the application        
    End Sub

    ' Subroutine that gets called when the network changes 
    Private Shared Sub OnNetworkChange (ByVal sender As object, _
       ByVale AS EventArgs)

       If NetworkInterface.GetIsNetworkAvailable() Then
           If Not online Then
              online = True
              ' do what is needed to GoOnline()
           End If
       Else 
           If online Then
               online = False
               ' do what is needed to GoOffline()
           End If
       End If
    End Sub

    Private Shared Sub SetupNetworkChange()
        ' Get current network availalable and store the 
        ' initial value for online value
        If NetworkInterface.GetIsNetworkAvailable() Then
            online = true
            ' do what is needed to GoOnline()
        Else    
            online = false
            ' do what is needed to GoOffline()
        End If    

        ' Now add a network change event handler to indicate
        ' network availability 
        AddHandler NetworkChange.NetworkAddressChanged , _
            AddressOf OnNetworkChange
    End Sub

End Class  


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.