c# – 如何检查通用Windows平台上的互联网连接类型
我想在
Windows通用应用程序中检查互联网连接类型.
>未连接 以便提供下载大尺寸内容的选项.并且还能感知网络可用性的重大变化. 目前,我只能使用NetworkInterface类的GetIsNetworkAvailable方法检查互联网是否连接. NetworkInterface.GetIsNetworkAvailable(); 解决方法
1.检查Internet连接可用性
要检查互联网是否连接使用GetInsNetworkAvailable方法的NetworkInterface类. bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();
2.通过WWLN(WiFi)检查Internet连接的可用性 要检查通过WWAN连接的互联网是否使用ConnectionProfile类的IsWlanConnectionProfile属性 ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWlanConnectionProfile;
3.通过WWAN(手机)检查Internet连接的可用性 检查通过WWAN连接的互联网是否使用ConConnectionProfile类的IsWwanConnectionProfile属性 ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWwanConnectionProfile;
参考 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 } 参考(更详细的回答) 5.管理网络可用性更改 要感知重要的网络可用性更改,请使用NetworkInformation类的eventNetworkStatusChanged // 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 = ""; } 参考 希望对某人有帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |