如何在通用 Windows 平台中检查互联网连接类型
How to check internet connectivity type in Universal Windows Platform
我想在 Windows 通用应用程序中检查互联网连接类型。
- 未连接
- 已通过 WLAN(WiFi) 连接
- 已通过 WWAN(蜂窝数据)连接
- 已连接到按流量计费的网络
为了提供下载大尺寸内容的选项。并感知网络可用性的重大变化。
目前,我只能使用 NetworkInterface
class.
的 GetIsNetworkAvailable
方法检查是否已连接互联网
NetworkInterface.GetIsNetworkAvailable();
我使用 NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfile
和 IsWwanConnectionProfile
。如果两者都不是 true
,则应该表示您在以太网或类似网络上。
请记住,GetInternetConnectionProfile()
可以 return 为空,并且可以在连接处于活动状态但 DHCP 失败时错误地 return 存在活动的互联网连接。
1。检查 Internet 连接可用性
要检查是否建立了任何网络连接,请使用 NetworkInterface
的 GetIsNetworkAvailable
方法 class。
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) 检查 Internet 连接可用性
要检查是否通过 WWAN 连接互联网,请使用 IsWlanConnectionProfile
属性 of ConnectionProfile
class
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 连接互联网,请使用 IsWwanConnectionProfile
属性 of ConnectionProfile
class
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.
参考
4。检查按流量计费的网络
要检查是否可以通过按流量计费的连接访问 Internet,请在 NetworkInterface
class 上使用 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
class
// 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
希望对大家有所帮助。
查找用户是否有任何网络连接(包括没有互联网的)我使用
public bool ConnectedToNetwork()
{
return NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter != null;
}
我想在 Windows 通用应用程序中检查互联网连接类型。
- 未连接
- 已通过 WLAN(WiFi) 连接
- 已通过 WWAN(蜂窝数据)连接
- 已连接到按流量计费的网络
为了提供下载大尺寸内容的选项。并感知网络可用性的重大变化。
目前,我只能使用 NetworkInterface
class.
GetIsNetworkAvailable
方法检查是否已连接互联网
NetworkInterface.GetIsNetworkAvailable();
我使用 NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfile
和 IsWwanConnectionProfile
。如果两者都不是 true
,则应该表示您在以太网或类似网络上。
请记住,GetInternetConnectionProfile()
可以 return 为空,并且可以在连接处于活动状态但 DHCP 失败时错误地 return 存在活动的互联网连接。
1。检查 Internet 连接可用性
要检查是否建立了任何网络连接,请使用 NetworkInterface
的 GetIsNetworkAvailable
方法 class。
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) 检查 Internet 连接可用性
要检查是否通过 WWAN 连接互联网,请使用 IsWlanConnectionProfile
属性 of ConnectionProfile
class
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 连接互联网,请使用 IsWwanConnectionProfile
属性 of ConnectionProfile
class
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.
参考
4。检查按流量计费的网络
要检查是否可以通过按流量计费的连接访问 Internet,请在 NetworkInterface
class 上使用 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
class
// 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
希望对大家有所帮助。
查找用户是否有任何网络连接(包括没有互联网的)我使用
public bool ConnectedToNetwork()
{
return NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter != null;
}