How to determine if internet connection is available?
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.
using Windows.Networking.Connectivity;
public static bool IsInternetConnected()
{
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
bool internet = (connections != null) &&
(connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
return internet;
}