c# - 如何在通用 Windows 平台中检查互联网连接类型

标签 c# win-universal-app windows-10-universal

我想在 Windows 通用应用程序中检查互联网连接类型。

  1. 未连接
  2. 通过无线局域网 (WiFi) 连接
  3. 通过 WWAN(蜂窝数据)连接
  4. 已连接到按流量计费的网络

为了提供下载大尺寸内容的选项。并感知网络可用性的重大变化。

目前,我只能使用 NetworkInterface 类的 GetIsNetworkAvailable 方法检查互联网是否连接。

NetworkInterface.GetIsNetworkAvailable();

最佳答案

1。检查互联网连接可用性

要检查是否建立了任何网络连接,请使用 NetworkInterface 类的 GetIsNetworkAvailable 方法。

bool isNetworkConnected = NetworkInterface.GetIsNetworkAvailable();

GetIsNetworkAvailable() -
Summary: Indicates whether any network connection is available.
Returns: true if a network connection is available; otherwise, false.


2。通过 WWLN (WiFi) 检查互联网连接可用性

要检查是否通过 WWAN 连接互联网,请使用 ConnectionProfile 类的 IsWlanConnectionProfile 属性

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWlanConnectionProfile;

IsWlanConnectionProfile
Summary: Gets a value that indicates if connection profile is a WLAN (WiFi) connection. This determines whether or not WlanConnectionProfileDetails is null.
Returns: Indicates if the connection profile represents a WLAN (WiFi) connection.


3。通过 WWAN(移动)检查 Internet 连接可用性

要检查是否通过 WWAN 连接互联网,请使用 ConnectionProfile 类的 IsWwanConnectionProfile 属性

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWwanConnectionProfile;

IsWwanConnectionProfile
Summary: Gets a value that indicates if connection profile is a WWAN (mobile) connection. This determines whether or not WwanConnectionProfileDetails is null.
Returns: Indicates if the connection profile represents a WWAN (mobile) connection.

引用
Hippiehunter Answer


4。检查计量网络

要检查是否可以通过按流量计费的连接访问 Internet,请使用 NetworkInterface 类上的 GetConnectionCost 方法。

var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if (connectionCost.NetworkCostType == NetworkCostType.Unknown 
        || connectionCost.NetworkCostType == NetworkCostType.Unrestricted)
{
    //Connection cost is unknown/unrestricted
}
else
{
   //Metered Network
}

引用(更详细的答案在这里)
1. How to manage metered network cost constraints - MSDN
2. NetworkCostType Enum - MSDN


5。管理网络可用性变化

要感知显着的网络可用性变化,请使用 NetworkInformation 类的事件NetworkStatusChanged

// register for network status change notifications
 networkStatusCallback = new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
 if (!registeredNetworkStatusNotif)
 {
     NetworkInformation.NetworkStatusChanged += networkStatusCallback;
     registeredNetworkStatusNotif = true;
 }

async void OnNetworkStatusChange(object sender)
{
    // get the ConnectionProfile that is currently used to connect to the Internet                
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

    if (InternetConnectionProfile == null)
    {
        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
        });
    }
    else
    {
        connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            rootPage.NotifyUser(connectionProfileInfo, NotifyType.StatusMessage);
        });
    }
    internetProfileInfo = "";
}

引用资料
Check Internet Connectivity - developerinsider.co

How to manage network connection events and changes in availability - MSDN

How to retrieve network connection information- MSDN


希望对大家有帮助。

关于c# - 如何在通用 Windows 平台中检查互联网连接类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35901526/

相关文章:

c# - 如何使这个 Dictionary TryGetValue 代码更具可读性?

c# - 如何隐藏 DataGridViewComboBoxColumn 的下拉箭头,如 Visual Studio Properties 窗口?

c# - 唯一设备 ID UWP

win-universal-app - 使用 UWP 读取 QR 条码 - Windows 10

c# - gl.DrawArrays 中的 AccessViolationException

c# - 无法从 DeviceInformation.Id 创建 UsbDevice

c# - 读取来自 HttpClient.GetStringAsync 的响应

xaml - Windows 10 Mobile 软键盘高度

c# - 如何从另一个 SoftwareBitmap (UWP) 的区域创建 SoftwareBitmap

c# - 可能对标有 notnull 属性的实体进行空赋值