加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:42:11 所属栏目:百科 来源:网络整理
导读:我想在 Windows通用应用程序中检查互联网连接类型. 未连接 通过WLAN连接(WiFi) 通过WWAN(蜂窝数据)连接 连接到计量网络 以便提供下载大尺寸内容的选项.并且还能感知网络可用性的重大变化. 目前,我只能使用NetworkInterface类的GetIsNetworkAvailable方法检查
我想在 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 Internetn",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/

希望对某人有帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读