检查Internet连接是否可用C#

什么是最简单的方法来检查互联网连接是否可用编程方式?

编辑:build议我尝试使用下面的方法,但它总是返回true。

[Flags] enum InternetConnectionState : int { INTERNET_CONNECTION_MODEM = 0x1, INTERNET_CONNECTION_LAN = 0x2, INTERNET_CONNECTION_PROXY = 0x4, INTERNET_RAS_INSTALLED = 0x10, INTERNET_CONNECTION_OFFLINE = 0x20, INTERNET_CONNECTION_CONFIGURED = 0x40 } class Program { [DllImport("WININET", CharSet = CharSet.Auto)] static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved); static void Main(string[] args) { InternetConnectionState flags = 0; bool isConnected = InternetGetConnectedState(ref flags, 0); Console.WriteLine(isConnected); //Console.WriteLine(flags); Console.ReadKey(); } } 

附加信息(如果有帮助):我通过共享的WiFinetworking访问互联网。

这里是你可以调用的Windows API。 它在wininet.dll中,并称为InternetGetConnectedState 。

 using System; using System.Runtime; using System.Runtime.InteropServices; public class InternetCS { //Creating the extern function... [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState( out int Description, int ReservedValue ); //Creating a function that uses the API function... public static bool IsConnectedToInternet( ) { int Desc ; return InternetGetConnectedState( out Desc, 0 ) ; } } 

Microsoft Windows Vista和7使用NCSI(networking连接状态指示器)技术:

NCSI在www.msftncsi.com上执行DNS查询,然后请求http://www.msftncsi.com/ncsi.txt 。 该文件是纯文本文件,仅包含文本“Microsoft NCSI”。 NCSI向dns.msftncsi.com发送DNS查找请求。 这个DNS地址应该parsing为131.107.255.255。 如果地址不匹配,则认为互联网连接无法正常工作。

您可以使用.NET 2.0+来检查networking连接

 System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); 

这可能只会返回本地networking,所以它可能不适合你。

这是迄今为止我发现的最好的解决scheme:

 public static bool isConnected() { try { string myAddress = "www.google.com"; IPAddress[] addresslist = Dns.GetHostAddresses(myAddress); if (addresslist[0].ToString().Length > 6) { return true; } else return false; } catch { return false; } } 

用法:

 if(isConnected()) { //im connected to the internet } else { //not connected } 

这是一个问题,答案真的是“取决于”。 因为这取决于你为什么要检查和连接什么样的? 你想通过http访问某些网站/服务吗? 发送SMTP邮件? 做DNS查询?

结合以前的答案可能是要走的路 – 首先使用来自答案的答案的wininet API来检查是否有任何types的连接可用。

如果是这样,请尝试几个DNS查找(请参阅System.Net.Dns)为您感兴趣的资源或一些stream行的大型网站(谷歌,altavista,计算机等)。

接下来,您可以尝试ping(请参阅Roger Willcocks的答案)和/或build立到相同站点的套接字连接。 请注意,失败的ping可能意味着防火墙规则不允许您ping。

如果你可以更具体地说明你为什么要检查它会更容易提供一个涵盖你的要求的答案…

最简单的方法是检查他们是否可以从网上下载一个你知道可用的文件。 使用下面的代码下载谷歌的主页。

 WebClient Client = new WebClient (); String Response; Response = Client.DownloadString("http://www.google.com"); 

您可能应该将其包装在Try …. Catch中以捕获无法build立连接时引发的exception。

networking意识在Windows XP中

networking意识在Windows Vista和Windows 7中

当然,这并不能保证你打算访问的网站不会被合作的防火墙规则阻塞, 只是检查一个预定义的网站是否可以访问 。

还有一些其他的select。

InfoPath库公开了“IsDestinationReachable”方法,该方法是具有相同名称的Win API方法的包装。 (看起来像Vista不支持)

不过,我相信从Docs来看,它依赖于PING这样的东西,所以不一定是一个好的testing。

Vista +说使用“networking列表pipe理器”

谷歌可能不会closures,但如果DNS lokup间隔超过超时,事情也可能会失败。 在新西兰家庭并不罕见,首先尝试失败,第二次成功,因为在第二次请求完成时,数据已经传播回ISP DNS或您的路由器caching。

或者,您可以使用DNS查找来检查互联网连接。 这种方法比Ping方法快。

 public static bool DnsTest() { try { System.Net.IPHostEntry ipHe = System.Net.Dns.GetHostByName("www.google.com"); return true; } catch { return false; } }