17

How to check internet connection availability in Windows 8,C# development ? I looked at MSDN but page has been deleted.

mert
  • 920
  • 3
  • 15
  • 24
  • 1
    *Duplicate of:* http://stackoverflow.com/a/11797256/763026. Should be Closed. – Angshuman Agarwal Nov 29 '12 at 12:03
  • You would do exactly what you would do on Windows 7, Windows Vista, or Windows XP attempt to get the content and if it failed display an error message. – Security Hound Nov 29 '12 at 12:52
  • possible duplicate of [Network checking in WinRT(C# implementation)](http://stackoverflow.com/questions/11797133/network-checking-in-winrtc-implementation) – Kate Gregory Nov 29 '12 at 15:49

3 Answers3

42

I use this snippet without problems:

public static bool IsInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}
Martin Suchan
  • 10,600
  • 3
  • 36
  • 66
  • 1
    Hi Martin, how can I achieve the same thing in windows phone 8? I get get "Method or operation not implemented" exception in windows phone 8 using your snippet. Thanks in advance – yayadavid Jan 02 '14 at 13:08
  • 3
    This does not work if there are multiple connections, which e.g. may be the case in the emulators. Use the code from Joshua Heller below, which fixes the problem. – sibbl Oct 18 '15 at 14:42
  • Works with Windows 10 SDK 1803 Build 17134 – Emy Blacksmith May 20 '19 at 13:19
10

I had to use GetConnectionProfiles() and GetInternetConnectionProfile() to make it work across all devices.

class ConnectivityUtil
{
    internal static bool HasInternetConnection()
    {            
        var connections = NetworkInformation.GetConnectionProfiles().ToList();
        connections.Add(NetworkInformation.GetInternetConnectionProfile());

        foreach (var connection in connections)
        {
            if (connection == null)
                continue;

            if (connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
                return true;
        }

        return false;
    }
}
Joshua Heller
  • 121
  • 1
  • 6
  • This also works if there are multiple internet connections - which may be the case in the Win 10 emulator and other circumstances. – sibbl Oct 18 '15 at 14:43
0

For Windows Phone Following code may be usefull:

var networkInformation = NetworkInformation.GetConnectionProfiles();    
if (networkInformation.Count == 0)    
{    
  //no network connection    
}          
Hariharan
  • 24,741
  • 6
  • 50
  • 54
Vishal Jadhav
  • 101
  • 1
  • 2
  • 11