方法其实很简单,用的是System.Net.NetworkInformation.Ping与System.Net.NetworkInformation.PingReply。也就是ping方法。
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace isConnInternet
{
public class IsConnInt
{
public bool isConn()
{
System.Net.NetworkInformation.Ping ping;
System.Net.NetworkInformation.PingReply res;
ping = new System.Net.NetworkInformation.Ping();
try
{
res = ping.Send("www.baidu.com");
if (res.Status != System.Net.NetworkInformation.IPStatus.Success)
return false;
else
return true;
}
catch (Exception er)
{
return false;
}
}
}
}