判断当前机器是否连接到Internet
在WEB应用程序,利用ASP.net语言编写代码,来判断当前运行此代码的机器是否连接到互联网上,具体操作如下:
第一步:在自定义的类声明下先定义声明外部API函数(即先导入外部API函数)如下声明定义:
//检测网络连接是否连接到Internet
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
第二步:在自定义类中创建自定义的方法来调用导入的外部API函数来判断机器是否连接到Internet上,如下创建并调用:
public bool IsConnected()
{
int connectionDescription = 0;
//判断是否连接到外网上的函数,并返回布尔值
return InternetGetConnectedState(out connectionDescription, 0);
}