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

问题描述 投票:0回答:4

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

  1. 未连接
  2. 通过WLAN(WiFi)连接
  3. 通过 WWAN(蜂窝数据)连接
  4. 已连接至按流量计费的网络

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

目前,我只能使用

GetIsNetworkAvailable
类的
NetworkInterface
方法来检查互联网是否已连接。

NetworkInterface.GetIsNetworkAvailable();
c# win-universal-app windows-10-universal
4个回答
57
投票

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

要检查是否建立了任何网络连接,请使用

GetIsNetworkAvailable
类的
NetworkInterface
方法。

bool isNetworkConnected = NetworkInterface.GetIsNetworkAvailable();

GetIsNetworkAvailable() -
摘要: 指示是否有可用的网络连接。
返回:

true
如果网络连接可用;否则,
false


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

要检查互联网是否通过 WWAN 连接,请使用

IsWlanConnectionProfile
ConnectionProfile

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

IsWlanConnectionProfile
摘要: 获取一个值,该值指示连接配置文件是否为 WLAN (WiFi) 连接。这决定了是否 WlanConnectionProfileDetails 为空。
返回: 指示连接配置文件是否代表 WLAN (WiFi) 连接。


3.通过 WWAN(移动)检查互联网连接可用性

要检查互联网是否通过 WWAN 连接,请使用

IsWwanConnectionProfile
ConnectionProfile

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

IsWwanConnectionProfile
摘要: 获取一个值,该值指示连接配置文件是否为 WWAN(移动)连接。这确定 WwanConnectionProfileDetails 是否为 null。
返回: 指示连接配置文件是否代表 WWAN(移动)连接。

参考
嬉皮猎人答案


4.检查计量网络

要检查是否可以通过按流量计费的连接访问互联网,请在

GetConnectionCost
类上使用
NetworkInterface
方法。

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

参考(更详细的答案在这里)
1. 如何管理计量网络成本约束 - MSDN
2. NetworkCostType 枚举 - MSDN


5.管理网络可用性更改

要感知显着的网络可用性变化,请使用

NetworkStatusChanged
的事件
NetworkInformation

// 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 = "";
}

参考文献
检查互联网连接 -developerinsider.co

如何管理网络连接事件和可用性变化 - MSDN

如何检索网络连接信息- MSDN


希望对某人有帮助。


6
投票

我用

NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfile
IsWwanConnectionProfile
。如果两者都不是
true
,则应该意味着您正在以太网或类似的网络上。

请记住,

GetInternetConnectionProfile()
可能会返回 null,并且当连接处于活动状态但 DHCP 失败时,可能会错误地返回存在活动互联网连接。


1
投票

查找用户是否有任何网络连接(包括没有互联网的网络连接)我使用

public bool ConnectedToNetwork()
{
    return NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter != null;
}

0
投票

NetworkInterface.GetIsNetworkAvailable();
只告诉您是否已连接到网络 - 它不会告诉您是否已连接到互联网,正如答案之一所建议的那样(通过其标题)。

dwb提供的答案似乎是确定您是否连接到互联网的最佳方法。

© www.soinside.com 2019 - 2024. All rights reserved.