如何通过3G连接检查互联网连接状态?

时间:2012-08-01 10:14:43

标签: c# windows-8 microsoft-metro windows-runtime

我有以下方法:

public static bool IsNetworkConnected()
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    IReadOnlyList<ConnectionProfile> connectionProfile = NetworkInformation.GetConnectionProfiles();
    if (InternetConnectionProfile == null)
        return false;
    else
        return true;
}

当我以典型方式连接到互联网时 - 通过LAN电缆或Wi-Fi,它可以正常工作。当我使用我的3G USB调制解调器时,它返回false(InternectConnectionProfile为空)。这是为什么?我该如何解决?

3 个答案:

答案 0 :(得分:4)

您可以考虑在互联网上ping服务器。这不是您问题的100%答案,但从不同的角度来看可能会有所帮助。

using System.Net;
using System.Net.NetworkInformation;

using (Ping Packet = new Ping()) 
{
    if (Packet.Send(IPAddress.Parse(IP), 1000, new byte[] { 0 }).Status ==   IPStatus.Success))
    {
       // ...
    }
}

答案 1 :(得分:2)

尝试

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

答案 2 :(得分:1)