Joe Sova adds:
This is a complex topic as there are many ways of doing this but none are
without side effects.
The registry value
"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\RemoteAccess" is True
if Online but only for modems.
InternetGetConnectedState works great if you're not on a Local Area
Network (LAN) but if you are on a LAN this will always return True whether
you are online or not.
A direct ping to a URL is very effective but is very, very slow if there
is no connection. Unable to get the "No Connection Timeout" to activate
after 5 seconds instead of the default 30-45 seconds!
Downloading a webpage is probably the most effective but it is quite
I think the problem is the disconnected philosophy of the Internet. The assumption is that a network link may disappear at any time. It makes the network robust but makes it hard to know whether the network is gone or just slow.