Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if internet connection is available?

How can I determine if an internet connection is available in windows store app?

like image 377
gurehbgui Avatar asked Feb 16 '13 08:02

gurehbgui


2 Answers

You can use the NetworkInformation class to detect that; this sample code adds an event handler that is called every time connection status changes;

NetworkInformation.NetworkStatusChanged += 
    NetworkInformation_NetworkStatusChanged; // Listen to connectivity changes

static void NetworkInformation_NetworkStatusChanged(object sender)
{
    ConnectionProfile profile = 
        NetworkInformation.GetInternetConnectionProfile();

    if ((profile != null) && profile.GetNetworkConnectivityLevel() >=
                NetworkConnectivityLevel.InternetAccess)
    {
        // We have Internet, all is golden
    }
}

Of course, if you want to just detect it once instead of getting notified when it changes, you can just do the check from above without listening to the change event.

like image 127
Joachim Isaksson Avatar answered Oct 24 '22 12:10

Joachim Isaksson


using Windows.Networking.Connectivity;      

public static bool IsInternetConnected()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = (connections != null) && 
        (connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
            return internet;
}
like image 37
Moaz Avatar answered Oct 24 '22 12:10

Moaz