我想检查 Windows 通用应用程序中的互联网连接类型。
为了提供下载大尺寸内容的选项。并且还可以感知网络可用性的重大变化。
目前,我只能使用
GetIsNetworkAvailable
类的NetworkInterface
方法来检查互联网是否已连接。
NetworkInterface.GetIsNetworkAvailable();
要检查是否建立了任何网络连接,请使用
GetIsNetworkAvailable
类的 NetworkInterface
方法。
bool isNetworkConnected = NetworkInterface.GetIsNetworkAvailable();
GetIsNetworkAvailable() -
摘要: 指示是否有可用的网络连接。
返回:如果网络连接可用;否则,true
。false
要检查互联网是否通过 WWAN 连接,请使用
IsWlanConnectionProfile
类的
ConnectionProfile
属性
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWlanConnectionProfile;
IsWlanConnectionProfile
摘要: 获取一个值,该值指示连接配置文件是否为 WLAN (WiFi) 连接。这决定了是否 WlanConnectionProfileDetails 为空。
返回: 指示连接配置文件是否代表 WLAN (WiFi) 连接。
要检查互联网是否通过 WWAN 连接,请使用
IsWwanConnectionProfile
类的
ConnectionProfile
属性
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWwanConnectionProfile;
IsWwanConnectionProfile
摘要: 获取一个值,该值指示连接配置文件是否为 WWAN(移动)连接。这确定 WwanConnectionProfileDetails 是否为 null。
返回: 指示连接配置文件是否代表 WWAN(移动)连接。
参考
嬉皮猎人答案
要检查是否可以通过按流量计费的连接访问互联网,请在
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
要感知显着的网络可用性变化,请使用
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
希望对某人有帮助。
我用
NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfile
和IsWwanConnectionProfile
。如果两者都不是 true
,则应该意味着您正在以太网或类似的网络上。
请记住,
GetInternetConnectionProfile()
可能会返回 null,并且当连接处于活动状态但 DHCP 失败时,可能会错误地返回存在活动互联网连接。
查找用户是否有任何网络连接(包括没有互联网的网络连接)我使用
public bool ConnectedToNetwork()
{
return NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter != null;
}
NetworkInterface.GetIsNetworkAvailable();
只告诉您是否已连接到网络 - 它不会告诉您是否已连接到互联网,正如答案之一所建议的那样(通过其标题)。
dwb提供的答案似乎是确定您是否连接到互联网的最佳方法。