Check internet connection (availability) in Window

2019-01-14 16:54发布

How to check internet connection availability in Windows 8,C# development ? I looked at MSDN but page has been deleted.

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-14 17:01

For Windows Phone Following code may be usefull:

var networkInformation = NetworkInformation.GetConnectionProfiles();    
if (networkInformation.Count == 0)    
{    
  //no network connection    
}          
查看更多
混吃等死
3楼-- · 2019-01-14 17:15

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;
    }
}
查看更多
不美不萌又怎样
4楼-- · 2019-01-14 17:20

I use this snippet without problems:

public static bool IsInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}
查看更多
登录 后发表回答