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

C# 如何通过3G连接检查互联网连接状态?,c#,windows-8,microsoft-metro,windows-runtime,C#,Windows 8,Microsoft Metro,Windows Runtime,我有以下方法: public static bool IsNetworkConnected() { ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); IReadOnlyList<ConnectionProfile> connectionProfile = NetworkInformation.GetConnectionProf

我有以下方法:

public static bool IsNetworkConnected()
{
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    IReadOnlyList<ConnectionProfile> connectionProfile = NetworkInformation.GetConnectionProfiles();
    if (InternetConnectionProfile == null)
        return false;
    else
        return true;
}
公共静态bool IsNetworkConnected()
{
ConnectionProfile InternetConnectionProfile=网络信息。GetInternetConnectionProfile();
IReadOnlyList connectionProfile=网络信息。GetConnectionProfiles();
如果(InternetConnectionProfile==null)
返回false;
其他的
返回true;
}
当我通过局域网电缆或Wi-Fi以典型方式连接到互联网时,它工作正常。当我使用3G USB调制解调器时,它返回false(
InternetConnectionProfile
为空)。为什么呢?我怎样才能修好它?

这可能会对您有所帮助
你可以考虑在互联网上打一个服务器。这不是你问题的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))
    {
       // ...
    }
}
试一试


由于路由器之间的数据包有时似乎会因喝咖啡而停止并丢失,因此在报告“连接中断”之前,我会尝试ping至少3次,否则我同意这种简单的方法。我通常建议您忽略当前的连接状态,尝试在internet上执行任何您想执行的操作(最好是在后台线程中)-然后,如果在(超时时间/重试次数)之后无法完成任务,则向用户报告。连接可能会在检查它们的存在和使用它们之间中断,也可能在完成任何任务的中途中断。由于您必须编写代码来应对这些情况,因此请保存一些代码并避免预检查。
public static bool IsConnectedToInternet()
{
        ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
        return (connectionProfile!=null && connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
}