如何在UWP中检查有线网络连接?

时间:2016-06-23 08:19:04

标签: c# .net networking uwp

如何查找用户是否使用有线连接到互联网?

以下是我目前的进展

var internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();

if (internetConnectionProfile.GetNetworkConnectivityLevel() == Windows.Networking.Connectivity.NetworkConnectivityLevel.InternetAccess)
{
    // has internet connection
}
if (internetConnectionProfile.IsWwanConnectionProfile)
{
    // its mobile
}
if (internetConnectionProfile.IsWlanConnectionProfile)
{
    // its wireless
}

2 个答案:

答案 0 :(得分:2)

试试这个

bool IsWiredInternetAccess()
    {
IReadOnlyList<ConnectionProfile> connections = NetworkInformation.GetConnectionProfiles();

        foreach (var connection in connections)
     {
            if (connection == null) continue;

            if (connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
        {
       // check is connection wired
       if ((connection.ConnectionProfile.IsWlanConnectionProfile)||(connection.IsWwanConnectionProfile))
          {
           // connection is Wlan or Wwan
           return false;
          }
       else
          {
           return true;
          }
        }       
     }
    }

答案 1 :(得分:0)

这是一个简单且更新的答案

更新的ConnectionProfile类有助于识别网络连接的类型。

ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWifi = connectionProfile.IsWlanConnectionProfile;