我有以下方法:
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
为空).这是为什么?我该如何解决?
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?
您可以考虑在 Internet 上 ping 一个服务器.这不是您问题的 100% 答案,但从不同的角度来看可能会有所帮助.
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))
{
// ...
}
}