在项目中用到了网络检测,有两种方法可以用。、
1、ping方法
Ping ping = new Ping();
PingReply reply = ping.Send(可以访问的网址,超时毫秒数);
if (reply.Status == IPStatus.Success)
连接正常;
2、调用系统dll
[DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int nFlags, int nReserved);
public static bool CheckInternet()
{
int nFlags = 0;
return InternetGetConnectedState(ref nFlags, 0);
}