【问题标题】:Check internet connection (availability) in Windows 8在 Windows 8 中检查 Internet 连接(可用性)
【发布时间】:2023-03-10 14:27:01
【问题描述】:

如何在 Windows 8、C# 开发中检查 Internet 连接可用性?我查看了 MSDN,但页面已被删除。

【问题讨论】:

标签: c# windows-8 microsoft-metro


【解决方案1】:

我必须使用 GetConnectionProfiles() 和 GetInternetConnectionProfile() 才能使其在所有设备上工作。

class ConnectivityUtil
{
    internal static bool HasInternetConnection()
    {            
        var connections = NetworkInformation.GetConnectionProfiles().ToList();
        connections.Add(NetworkInformation.GetInternetConnectionProfile());

        foreach (var connection in connections)
        {
            if (connection == null)
                continue;

            if (connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
                return true;
        }

        return false;
    }
}

【讨论】:

  • 这也适用于有多个互联网连接的情况——在 Win 10 模拟器和其他情况下可能就是这种情况。
【解决方案2】:

对于 Windows Phone 以下代码可能有用:

var networkInformation = NetworkInformation.GetConnectionProfiles();    
if (networkInformation.Count == 0)    
{    
  //no network connection    
}          

【讨论】:

  • 不适合我。 networkInformation.Count.ToString() 在 Aereo 模式下返回 15...
【解决方案3】:

我用这个sn-p没有问题:

public static bool IsInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}

【讨论】:

  • 嗨马丁,我怎样才能在 windows phone 8 中实现同样的功能?我使用您的 sn-p 在 windows phone 8 中得到“方法或操作未实现”异常。提前致谢
  • 如果有多个连接,这不起作用,例如在模拟器中可能是这种情况。使用下面来自 Joshua Heller 的代码,它可以解决问题。
  • 适用于 Windows 10 SDK 1803 Build 17134
猜你喜欢
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多