You can use the following code to determine if you are online or not:

using System.Net.NetworkInformation;


public static bool IsOnline()
  bool isConnected = false;
  NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  foreach (NetworkInterface n in adapters)
    if (n.OperationalStatus == OperationalStatus.Up && n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
    // Since we have a non loopback interface up, we have a network connection
    isConnected = true;
  return isConnected;

Update: I added a “break” to the loop so that I exit as soon as I find a good connection.



Thanks Paul.

Here are the documentation for GetIsNetworkAvailable

Paul Lo

There is a more simple way:

bool isNetworkAvailable = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();


ignore me, I read it wrong :D


That function will only tell you if the last adapter in your collection is online.