如何在Universal Windows Platform中检查Internet连接类型
我想在Windows Universal Application中检查互联网连接类型。
- 未连接
- 通过WLAN(WiFi)连接
- 通过WWAN连接(蜂窝数据)
- 连接到计量网络
为了提供下载大尺寸内容的选项。 并且还可以感知重要的网络可用性变化。
目前,我只能使用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 Internetn", 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().IsWlanConnectionProfile
和IsWwanConnectionProfile
。 如果两者都不true
,那就意味着你在以太网上或类似的东西。
请记住, GetInternetConnectionProfile()
可以返回null,并且当连接处于活动状态但DHCP失败时,可能会错误地返回存在活动的Internet连接。
上述就是C#学习教程:如何在Universal Windows Platform中检查Internet连接类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1015559.html