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/

希望对某人有帮助

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...