Finding out the status of the computer's network connection status
is a fairly popular need with developers, and you'd think that there
would be a method in .NET to provide this information. Unfortunately,
.NET contains no in built method to detect and provide this
information. So, here are some tricks developers use to find out the
computers connection status:
-  Ping a valid IP address.
This appears to be the most foolproof method of checking the
connection status. Be sure to use a valid IP address (say
22.214.171.124) insted of a domain name (say www.google.com), since
using a domain name will cause the computer to start dialling the
network service provider if the computer is not connected. It will
also cause problems with dial-up internet connection sharing (the
"server" system will dial whenever one of the client systems does any
of these types of calls). And you don't want that, since it will
irritate your users no end.
There are a couple of methods you can use to ping an IP address:
- Use the WMI(Windows Management Instrumentation)
As specified in the below mentioned ExpertsExchange question,
this require only two lines of code. Here is the code...
dim x as new system.management.managementobject("Win32_PingStatus.Address='126.96.36.199'")
as given on this page:
- ExpertsExchange: pinging an IP using .NET framework
For more information on the Win32_PingStatus WMI class and its
return values, see the following MSDN Library article:
- MSDN : Platform SDK: WMI > Win32_PingStatus
- Another way to ping an IP address is by using the System.Net
classes. You can find example code in the following newsgroup threads:
- Subject: Ping in VB.net
- Subject: Can anyone convert this C# sample to VB.NET?
-  Use the InternetGetConnectedState() Win32 API function (also
suggested by hammer-ga in his comment)
Unfortunately, the InternetGetConnectedState() API(available with IE
4 and above) is not entirely reliable. It's exact functioning varies
with the version of IE installed, and in certain cases, it may be
unable to detect an internet connection, even when the computer is
For more information, please check out the following articles:
- MSKB Article 315035: The InternetGetConnectedState Function
Returns FALSE When Connected to the Internet
- MSKB Article 242558: Detecting If You Have a Connection to the
- InternetGetConnectedState Event Handler
-  Try connecting to a website/webpage (as also suggested by
sgtcory-ga in his comment)
This method unfortunately suffers from the dialling problem that I
mention while discussing the ping method. Every time you try to check
your online status this way, it will cause the host computer to dial
if it is not online.
See the following newsgroup thread:
- Subject: How to detect internet connection?
-  Check the computer's local IP address. If this address is the
loopback address (127.0.0.1) then the machine is not connected to a
network. Please see the following newsgroup post for the required
- Subject: Internet connection detect
Unfortunately, this method is also not foolproof. It will give
you an incorrect result if the user is on a network without being
connected to the Internet.
Hope this helps.
If you need any clarifications, just ask!
Google/Google Groups Search Terms Used:
detect internet connection
using ping to detect internet connection