1,第一种方法使用 wininet.dll中的 InternetGetConnectedState函数
此方法只有网络连接正常的情况下才返回true,如果虚拟机网络正常,本地网络不正常,那么返回false。
public static bool IsConnectedToInternet()
{
bool returnValue = false;
try
{
int Desc;
returnValue = NetApi.InternetGetConnectedState(out Desc, 0);
}
catch
{
returnValue = false;
}
return returnValue;
}
[DllImport("wininet.dll")] public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
2,使用System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
这个方法是本地网络连接中,只要有一个网络可用就为true,不管网络可不可以连接到外部网络