Search code examples
c#windows-runtimewindows-store-apps

How to determine if internet connection is available?


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


Solution

  • 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.