1 public static bool IsIPHostConnected()
2 {
3 try
4 {
5 System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com");
6 return true;
7 }
8 catch
9 {
10 // ignored
11 }
12 return false;
13 }
GetHostEntry函数,尝试将主机域名或者IP解析,如果解析失败,则电脑没有网络。
此函数判断网络连接,速度很快,基本<1ms。但是断网时执行的耗时有20多ms,相对偏高。