Check internet connection (availability) in Windows 8

13,769

Solution 1

I use this snippet without problems:

public static bool IsInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}

Solution 2

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;
    }
}
Share:
13,769

Related videos on Youtube

mert
Author by

mert

Updated on September 15, 2022

Comments

  • mert
    mert almost 2 years

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

  • yayadavid
    yayadavid over 10 years
    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
  • sibbl
    sibbl over 8 years
    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
    sibbl over 8 years
    This also works if there are multiple internet connections - which may be the case in the Win 10 emulator and other circumstances.
  • Emy Blacksmith
    Emy Blacksmith about 5 years
    Works with Windows 10 SDK 1803 Build 17134