步骤:
1、首先将以下代码拷贝到代码中:
[DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(
ref int dwFlag,
int dwReserved
);
2、引入命名空间
using System.Runtime.InteropServices;
3、代码中判断网络是否畅通,代码如下:
System.Int32 dwFlag = new int();
//在网络未连接情况下
if (!InternetGetConnectedState(ref dwFlag, 0))
{
//网络未连接情况下,处理业务逻辑
}
else
{
//网络连接情况下,处理业务逻辑
}
OK,只要按照上面的步骤来执行,应该就没有问题!