gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:26:14 34 4
gpt4 key购买 nike

我想在 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com