方法一
代码如下 |
复制代码 |
public bool CheckForInternetConnection()
{
bool result = false;
try
{
Ping ping = new Ping();
PingReply replay = ping.Send("114.114.114.114", 2000);
if (replay.Status == IPStatus.Success)
{
result = true;
}
}
catch { }
return result;
}
|
方法二
代码如下 |
复制代码 |
public bool CheckForInternetConnection()
{
bool result = false;
try
{
using (WebClient client = new WebClient())
{
client.DownloadString("http://www.111com.net/");
result = true;
}
}
catch { }
return result;
}
|
方法三
代码如下 |
复制代码 |
c#自带的Ping,using System.Net.NetworkInformation;
Ping ping = new Ping();
for (int i = iFrom; i <= iTo; i++)
{
mre1.WaitOne();
string ip = strIP + i.ToString();
label10.Text = "正在扫描主机:" + ip.ToString() ;
try
{
PingReply reply = ping.Send(ip, 100);
if (reply.Status == IPStatus.Success)
{
rtbInfo.AppendText("Host: " + ip.ToString() + "\r");
rtbInfo.ScrollToCaret();
}
}
catch {
rtbInfo.AppendText("发生了一个异常!\r");
}
}
rtbInfo.AppendText("扫描完毕!\r");
|
说明:NetworkInterface.GetIsNetworkAvailable()仅用于检测是否有可用的网络连接(网卡),不能用于检测计算机是否联网。例如拨号上网的计算机,没有拨号时也是返回True。