我有这个方法:
public static void testConnection()
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Windows.MessageBox.Show("This computer is connected to the internet");
}
else
{
System.Windows.MessageBox.Show("This computer is not connected to the internet");
}
}
我想它会告诉我连接是否可用,但它总是返回 true(并打印第一条消息),即使我确定没有连接。我做错了什么?
P.S.:我还在学习 C#。
最佳答案
我觉得这个方法比较合适:
public static bool getIsInternetAccessAvailable()
{
switch(NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel())
{
case NetworkConnectivityLevel.InternetAccess:
return true;
default:
return false;
}
}
关于c# - 为什么 GetIsNetworkAvailable() 总是返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457407/