Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

NetworkInterface.GetIsNetworkAvailable Method

Indicates whether any network connection is available.

Namespace:  System.Net.NetworkInformation
Assembly:  System.Net (in System.Net.dll)

[SecuritySafeCriticalAttribute]
public static bool GetIsNetworkAvailable()

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
{
  static bool online = false; 

  public static void Demo(System.Windows.Controls.TextBlock outputBlock)
  {

      // Initialize the online variable and set a NetworkChange handler
      SetupNetworkChange();

      outputBlock.Text += "Network is: ";
      if (online)
          outputBlock.Text += "online";
      else     
          outputBlock.Text += "offline";

      // Now start the main work of the application        
  }

  // method that gets called when the network changes
  static void OnNetworkChange(object sender, EventArgs e)
  {
     if (NetworkInterface.GetIsNetworkAvailable())
     {
         if (!online)
          {
             online = true;
             // do what is needed to GoOnline();
          }
     }
     else {
         if (online)
         {
             online = false;
             // do what is needed to GoOffline();
         }
     }
  }

  private static void SetupNetworkChange()
  {
     // Get current network availalability and store the 
     // initial value of the online variable
     if (NetworkInterface.GetIsNetworkAvailable())
     {
         online = true;
         // do what is needed to GoOnline();
     }
     else
     {
         online = false;
         // do what is needed to GoOffline();
     }

      // Now add a network change event handler to indicate
      // network availability 
      NetworkChange.NetworkAddressChanged +=
          new NetworkAddressChangedEventHandler(OnNetworkChange);
  }
}


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2014 Microsoft