How can I determine if an internet connection is available in windows store app?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With