public void NetInfo()
{
//lastTime = DateTimeOffset.Now;
var neti = NetworkInformation.GetInternetConnectionProfile();
NetworkInformation.NetworkStatusChanged += NetworkStatusChanged;
if (neti == null)
{
Comm.inetFlag = false;
SetControlVis(false);
}
else
{
var level = neti.GetNetworkConnectivityLevel();
UpdateNet(level);
}
}
private void NetworkStatusChanged(object sender)
{
if (timer != null)
{
timer.Dispose();
}
InetTimerSetup();
}
private void UpdateNet(NetworkConnectivityLevel level)
{
switch (level)
{
case NetworkConnectivityLevel.InternetAccess:
Comm.inetFlag = true;
SetControlVis(true);
break;
case NetworkConnectivityLevel.LocalAccess:
Comm.inetFlag = false;
SetControlVis(false);
break;
default:
Comm.inetFlag = false;
SetControlVis(false);
break;
}
}
private async void SetControlVis(bool state)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
if (state)
{
Ethernet.Visibility = Visibility.Visible;
Mynet.Visibility = Visibility.Collapsed;
Message.Visibility = Visibility.Collapsed;
ContentGrid.IsEnabled = true;
}
else
{
Ethernet.Visibility = Visibility.Collapsed;
Mynet.Visibility = Visibility.Visible;
ContentGrid.IsEnabled = false;
Message.Visibility = Visibility.Visible;
}
});
}
public void InetTimerSetup()
{
var period = (int)TimeSpan.FromSeconds(8).TotalMilliseconds;
timer = new Timer(TimerAsync, null, period, period);
}
private async void TimerAsync(object state)
{
var neti = NetworkInformation.GetInternetConnectionProfile();
if (neti == null)
{
Comm.inetFlag = false;
SetControlVis(false);
}
else
{
var level = neti.GetNetworkConnectivityLevel();
UpdateNet(level);
}
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
if (Comm.inetFlag && Comm.pageType == typeof(NetDown))
{
NavigationService.Navigate(typeof(ContentGridPage));
}
if (!Comm.inetFlag && (Comm.pageType == typeof(ContentGridPage)))
{
NavigationService.Navigate(typeof(NetDown));
}
});
}
UWP C# How to check is internet connection is available or not?
So I want to check whether the internet connection is available or not..
So I am using "NetworkInterface.GetIsNetworkAvailable()" but it gives me the false result... its return true but in real internet connection is not available
I want a way to check internet connetion is available or not
3 answers
Sort by: Most helpful
-
Groovykool 236 Reputation points
2020-02-11T05:15:34.65+00:00 -
Ivanich 306 Reputation points
2020-02-04T06:49:33.473+00:00 Try
NetworkInformation.GetInternetConnectionProfile() != null;
-
Peter Smith 581 Reputation points Microsoft Employee
2020-02-05T23:23:11.363+00:00 In general, the best way to check is to try the connection, and handle the failure (often by retrying with an exponential back-off). The reason is that "internet connectivity" is a surprisingly difficult thing to know: there's plenty of "weird" network configurations that will fool a simple test (and in either way).
For example, you might be behind a captive portal: every HTTP request you make return "200 OK", but in reality many of them are the portal trying to get the user to log in. Or some requests might fail, but others might pass (I've seen captive portals where simple requests were blocked by the portal, but more complex ones were let through. Or you might be in a school or business where some sites are allowed, and others are blocked. Or you might be in a country with a country-wide firewall. Or you could be in a place that allowed HTTP traffic, but not other TCP ports, or allows most TCP traffic but disallowed UDP.
Heck, where I am right now allows most internet traffic, but blocks Gopher (port 70) traffic.
Or you could have a VPN setup where the connection isn't made until it's requested by an app: until you ask to get on-line, it doesn't go on-line.
In short, just try to make the connection, and handle the error. It's what you'd have to do anyway for robustness. Your users will thank you!