Hi faith16-ga,
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:
- [1] 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
216.239.33.99) 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)
Win32_PingStatus class
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='65.5.21.23'")
msgbox x.properties("StatusCode").Value.ToString
=============================
as given on this page:
- ExpertsExchange: pinging an IP using .NET framework
classes
(http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20394521.html)
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
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp)
- 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
Newsgroup: microsoft.public.dotnet.languages.vb
(http://groups.google.com/groups?threadm=rwZt9.86018%24zE6.289161%40rwcrnsc51.ops.asp.att.net)
- Subject: Can anyone convert this C# sample to VB.NET?
Newsgroup: microsoft.public.dotnet.languages.vb
(http://groups.google.com/groups?threadm=eNav9.143113%24%25d2.51429%40sccrnsc01)
- [2] 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
online.
For more information, please check out the following articles:
- MSKB Article 315035: The InternetGetConnectedState Function
Returns FALSE When Connected to the Internet
(http://support.microsoft.com/?kbid=315035)
- MSKB Article 242558: Detecting If You Have a Connection to the
Internet
(http://support.microsoft.com/?kbid=242558)
- InternetGetConnectedState Event Handler
(http://66.129.67.100/247reference/msgs/5/28316.aspx)
- [3] 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?
Newsgroup: microsoft.public.dotnet.framework.windowsforms
(http://groups.google.com/groups?threadm=%23GWCO0xvBHA.2660%40tkmsftngp05)
- [4] 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
VB.NET code:
- Subject: Internet connection detect
Newsgroups: microsoft.public.dotnet.languages.vb
(http://groups.google.com/groups?selm=Odvz4%23TeCHA.1876%40tkmsftngp10)
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!
Regards,
Theta-ga
:-)
===========================================
Google/Google Groups Search Terms Used:
detect internet connection
InternetGetConnectedState
using ping to detect internet connection |