14

I have this method:

public static void testConnection()
    {
        if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            System.Windows.MessageBox.Show("This computer is connected to the internet");
        }
        else
        {
            System.Windows.MessageBox.Show("This computer is not connected to the internet");
        }
    }

I suppose it would tell me whether the connection is available or not but it always return true (and print the 1st message) even when I'm sure there is no connection. What I'm doing wrong?

P.S.: I'm still learning C#.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Rodrigo Guedes
  • 1,169
  • 2
  • 13
  • 27
  • @TimSchmelter I don't get it, it's a blank page. Oooooh ;) – Scott Solmer Oct 31 '14 at 21:46
  • 1
    `NetworkInterface.GetIsNetworkAvailable()` doesn't check internet connection. It only check whether any network connection is available. See this to check internet connection. https://stackoverflow.com/questions/5405895/how-to-check-the-internet-connection-with-net-c-and-wpf/60585095#60585095 – Mahbubur Rahman Mar 08 '20 at 14:18

6 Answers6

16

I think this method is more appropriate:

   public static bool getIsInternetAccessAvailable()
    {
        switch(NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel())
        {
            case NetworkConnectivityLevel.InternetAccess:
                return true;
            default:
                return false;
        }
    }
ventura8
  • 554
  • 6
  • 18
  • 3
    What is the using directive? I can not find the GetInternetConnectionProfile method. – Luca Ziegler Dec 16 '16 at 09:54
  • 1
    You need a null check on this `.GetInternetConnectionProfile()`. Pulling my network cable to simulate an outage resulted in it returning null, instead of an object that would produce a disconnected state from `GetNetworkConnectivityLevel()`. – Dan Is Fiddling By Firelight May 25 '18 at 19:20
  • 2
    This technique appears to be only available for Universal Windows Platform (UWP) projects. I tried searching for the Namespace in my desktop WPF project to no avail. – Bob Bryan Oct 16 '18 at 19:35
13

Please correct me if I am wrong but as far as I can see the method you are using is checking network connectivity and not necessarily internet connectivity. I would assume if you are on a network of any sort this would return true regardless of the internet being available or not? See this.

I have noticed that one way of checking for internet connectivity is as follows:

private bool IsInternetAvailable()
{
    try
    {
        Dns.GetHostEntry("www.google.com"); //using System.Net;
        return true;
    } catch (SocketException ex) {
        return false;
    }
}

The above code can be found (in VB.Net by reading the comment from Joacim Andersson [MVP]) in the following post.

Note: The latest edit was suggested by AceInfinity but was rejected in community review. My reputation is too low to override this so I made the change myself.

Community
  • 1
  • 1
Clarice Bouwer
  • 3,631
  • 3
  • 32
  • 55
4

Note that we are using the Windows.Networking.Connectivity.NetworkInformation and not the System.Net.NetworkInformation namespace.

public bool checkInternetAccess()
{
    var connectivityLevel = Windows.Networking.Connectivity.NetworkInformation
                           .GetInternetConnectionProfile().GetNetworkConnectivityLevel();

    return connectivityLevel == NetworkConnectivityLevel.InternetAccess;
}

Basically what ventura8 said. I would comment his solution, mentioning the namespaces, but I lack enough reputation.

humHann
  • 118
  • 1
  • 2
  • 7
3

From msdn:

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.

One of these examples could be your case:

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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 2
    To add, the remarks for GetIsNetworkAvailable() says 'A network connection is considered to be available if any network interface is marked "up" and is not a loopback or tunnel interface.' I believe this means it only looks at the network interface and not really the connection status for that interface. – Ravi Y Nov 19 '12 at 16:02
  • @ryadavilli agree, this also matters – Sergey Berezovskiy Nov 19 '12 at 16:02
1

From MSDN (emphasis is mine):

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

If with "connection" you mean Internet connection then you should DllImport the function InternetCheckConnection or InternetQueryOption.

Instead if what you need is just to know if computer is connected to any useful LAN the best thing you can do is to iterate network connection by yourself and to filter (using NetworkInterfaceType, IsReceiveOnly and OperationalStatus) what you're not interested too.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
1

I suppose it would tell me whether the connection is available or not but it always return true (and print the 1st message) even when I'm sure there is no connection.

You have a different opinion on the meaning of 'connection' than the manual does. As far as the operating system is concerned, you have a connection when there is a patched ethernet cable connected to your NIC, or when your wireless card is connected to an wireless access point, or any connection is active.

The manual also explains this:

NetworkInterface.GetIsNetworkAvailable:

Indicates whether any network connection is available.

If you want to detect internet connectivity, take a look at the InternetGetConnectedState() (or InternetCheckConnection(), to check for accessibility of a specific host) methods from the WinINet API.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • InternetCheckConnection is deprecated (it actually only does a ping, which .net has easier ways to do). – Poul Bak Jul 22 '19 at 17:51