Check internet connection (availability) in Windows 8

后端 未结 3 1811
天命终不由人
天命终不由人 2020-12-06 01:19

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

相关标签:
3条回答
  • 2020-12-06 01:38

    I use this snippet without problems:

    public static bool IsInternet()
    {
        ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
        bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
        return internet;
    }
    
    0 讨论(0)
  • 2020-12-06 01:44

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 01:53

    For Windows Phone Following code may be usefull:

    var networkInformation = NetworkInformation.GetConnectionProfiles();    
    if (networkInformation.Count == 0)    
    {    
      //no network connection    
    }          
    
    0 讨论(0)
提交回复
热议问题