How to check internet connection availability in Windows 8,C# development ? I looked at MSDN but page has been deleted.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I use this snippet without problems:
public static bool IsInternet()
{
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
return internet;
}
回答2:
I had to use GetConnectionProfiles() and GetInternetConnectionProfile() to make it work across all devices.
class ConnectivityUtil
{
internal static bool HasInternetConnection()
{
var connections = NetworkInformation.GetConnectionProfiles().ToList();
connections.Add(NetworkInformation.GetInternetConnectionProfile());
foreach (var connection in connections)
{
if (connection == null)
continue;
if (connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
return true;
}
return false;
}
}
回答3:
For Windows Phone Following code may be usefull:
var networkInformation = NetworkInformation.GetConnectionProfiles();
if (networkInformation.Count == 0)
{
//no network connection
}