Check internet connection (availability) in Windows 8

后端 未结 3 1817
天命终不由人
天命终不由人 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: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;
        }
    }
    

提交回复
热议问题