c# – 如何检查通用Windows平台上的互联网连接类型

前端之家收集整理的这篇文章主要介绍了c# – 如何检查通用Windows平台上的互联网连接类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在 Windows通用应用程序中检查互联网连接类型.

>未连接
>通过WLAN连接(WiFi)
>通过WWAN(蜂窝数据)连接
>连接到计量网络

以便提供下载大尺寸内容的选项.并且还能感知网络可用性的重大变化.

目前,我只能使用NetworkInterface类的GetIsNetworkAvailable方法检查互联网是否连接.

NetworkInterface.GetIsNetworkAvailable();

解决方法

1.检查Internet连接可用性

要检查互联网是否连接使用GetInsNetworkAvailable方法的NetworkInterface类.

bool isInternetConnected = 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连接的互联网是否使用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连接的互联网是否使用ConConnectionProfile类的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
}

参考(更详细的回答)
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/JJ835821(v=win.10).aspx
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类的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 Internet\n",NotifyType.StatusMessage);
        });
    }
    else
    {
        connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
        await _cd.RunAsync(CoreDispatcherPriority.Normal,() =>
        {
            rootPage.NotifyUser(connectionProfileInfo,NotifyType.StatusMessage);
        });
    }
    internetProfileInfo = "";
}

参考
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj835820.aspx
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh452991.aspx
http://www.developerinsider.in/2016/03/13/check-internet-connectivity-in-uwp/

希望对某人有帮助

原文链接:https://www.f2er.com/csharp/94954.html

猜你在找的C#相关文章