在手机上查看互联网连接
我想检查一下我的手机是否可以连接到互联网。 我已经看过几个问题。 其中一个是问题 。 它说使用NetworkInterface.GetIsNetworkAvailable()
这个,我尝试了。 我已将我的电脑与互联网断开连接,并且还关闭了模拟器的DataConnection
但NetworkInterface.GetIsNetworkAvailable()
这始终返回true。 但同时我还要检查NetworkInterfaceType.None
,有趣的是它会变为null。 任何人都可以解释我在哪里缺少信息?
尝试 : –
public static void CheckNetworkAvailability() { // this is coming true even when i disconnected my pc from internet. // i also make the dataconnection off of the emulator var fg = NetworkInterface.GetIsNetworkAvailable(); var ni = NetworkInterface.NetworkInterfaceType; // this part is coming none if (ni == NetworkInterfaceType.None) IsConnected = false; }
任何帮助表示赞赏:)
即使您将网络条件模拟为没有网络,模拟器也始终将NetworkInterface.GetIsNetworkAvailable()
返回true。
我自己遇到了这个问题,测试此行为的唯一真正方法是将应用程序部署到运行Windows Phone的物理设备,并在关闭数据的情况下对其进行测试。
我正在使用以下代码检查设备是否可以访问互联网,如果它连接到wifi或数据连接..
public void UpdateNetworkInformation() { // Get current Internet Connection Profile. ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); //air plan mode is on... if (internetConnectionProfile == null) { Is_Connected = false; return; } //if true, internet is accessible. this.Is_InternetAvailable = internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess; // Check the connection details. else if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != 71)// Connection is not a Wi-Fi connection. { Is_Roaming = internetConnectionProfile.GetConnectionCost().Roaming; /// user is Low on Data package only send low data. Is_LowOnData = internetConnectionProfile.GetConnectionCost().ApproachingDataLimit; //User is over limit do not send data Is_OverDataLimit = internetConnectionProfile.GetConnectionCost().OverDataLimit; } else //Connection is a Wi-Fi connection. Data restrictions are not necessary. { Is_Wifi_Connected = true; } }
编辑:对于简单的互联网连接,您可以使用下面的代码。
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
希望这可以帮助!
NetworkInterface.GetIsNetworkAvailable()
检查network connection
而不是internet connection
。 如果您在任何类型的网络中,那么无论internet
是否存在,它都将返回。
您可以按如下方式检查互联网连接:
using System.Net private bool IsOnline() { try { IPHostEntry iPHostEntry = Dns.GetHostEntry("www.wikipedia.com"); return true; } catch (SocketException ex) { return false; } }