Why is GetIsNetworkAvailable() always returning true?
From msdn:
There are many cases in which a device or computer is not connected to a useful network but is still considered available and GetIsNetworkAvailable will return true.
One of these examples could be your case:
For example, if the device running the application is connected to a wireless network that requires a proxy, but the proxy is not set, GetIsNetworkAvailable will return true. Another example of when GetIsNetworkAvailable will return true is if the application is running on a computer that is connected to a hub or router where the hub or router has lost the upstream connection.
I think this method is more appropriate:
public static bool getIsInternetAccessAvailable()
{
switch(NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel())
{
case NetworkConnectivityLevel.InternetAccess:
return true;
default:
return false;
}
}
Note that we are using the Windows.Networking.Connectivity.NetworkInformation
and not the System.Net.NetworkInformation
namespace.
public bool checkInternetAccess()
{
var connectivityLevel = Windows.Networking.Connectivity.NetworkInformation
.GetInternetConnectionProfile().GetNetworkConnectivityLevel();
return connectivityLevel == NetworkConnectivityLevel.InternetAccess;
}
Basically what ventura8 said. I would comment his solution, mentioning the namespaces, but I lack enough reputation.
Please correct me if I am wrong but as far as I can see the method you are using is checking network connectivity and not necessarily internet connectivity. I would assume if you are on a network of any sort this would return true regardless of the internet being available or not? See this.
I have noticed that one way of checking for internet connectivity is as follows:
private bool IsInternetAvailable()
{
try
{
Dns.GetHostEntry("www.google.com"); //using System.Net;
return true;
} catch (SocketException ex) {
return false;
}
}
The above code can be found (in VB.Net by reading the comment from Joacim Andersson [MVP]) in the following post.
Note: The latest edit was suggested by AceInfinity but was rejected in community review. My reputation is too low to override this so I made the change myself.