How to check internet connection status over 3G co

2019-02-15 23:56发布

I have the following method:

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

It works fine when I'm connected to the internet in a typical way - via LAN cable or Wi-Fi. When I'm using my 3G USB modem it returns false (InternectConnectionProfile is null). Why is that? How can I fix it?

3条回答
Deceive 欺骗
2楼-- · 2019-02-16 00:27

Try

public static bool IsConnectedToInternet()
{
        ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        return (connectionProfile!=null && connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
}
查看更多
smile是对你的礼貌
4楼-- · 2019-02-16 00:46

You may consider pinging a server on the internet. This is not the 100% answer to your question but may be helpful from a different point of view.

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))
    {
       // ...
    }
}
查看更多
登录 后发表回答