如何在Universal Windows Platform中检查Internet连接类型

我想在Windows Universal Application中检查互联网连接类型。

  1. 未连接
  2. 通过WLAN(WiFi)连接
  3. 通过WWAN连接(蜂窝数据)
  4. 连接到计量网络

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

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

 NetworkInterface.GetIsNetworkAvailable(); 

1.检查Internet连接可用性

检查Internet是否连接使用NetworkInterface类的GetIsNetworkAvailable方法。

 bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable(); 

GetIsNetworkAvailable()
摘要:指示是否有任何网络连接可用。
返回:如果网络连接可用,则返回 true ; 否则,是的。


2.通过WWLN(WiFi)检查Internet连接可用性

检查通过WWAN连接的Internet是否使用ConnectionProfile类的IsWlanConnectionProfile属性

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

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


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

检查通过WWAN连接的Internet是否使用ConnectionProfile类的IsWwanConnectionProfile属性

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

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

参考
Hippiehunter答案


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. https://msdn.microsoft.com/en-us/library/windows/apps/xaml/JJ835821(v=win.10).aspx
2. https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.networking.connectivity.networkcosttype.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet- 1


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

参考
https://developerinsider.co/check-internet-connectivity-in-uwp/ https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj835820.aspx https://msdn.microsoft .COM / EN-US /库/窗/应用/ XAML / hh452991.aspx


希望对某人有帮助。

我使用NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfileIsWwanConnectionProfile 。 如果两者都不true ,那就意味着你在以太网上或类似的东西。

请记住, GetInternetConnectionProfile()可以返回null,并且当连接处于活动状态但DHCP失败时,可能会错误地返回存在活动的Internet连接。