In the June column of .NET Tips, my predecessor, Cathi, had a Tip on how to determine if your computer is
connected to the Internet. As usual, with .NET there's always more than one way to skin a cat. Here's a
different method to determine the same thing.
public void TestInternetConnection()
{
InternetConnectionState flags = 0;
bool bInternet = InternetGetConnectedState(ref flags,0);
if (bInternet == false)
{
//Not Connected
if (flags == 0)
{
MessageBox.Show("Do something to tell the user to install some internet connection");
}
}
else
{
MessageBox.Show("You should be connected to the internet");
}
}
// Assumes you're using System.Runtime.InteropServices
[DllImport("wininet.dll",CharSet=CharSet.Auto)]
static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);
[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
}
from a solution provided by Einar Kvandahl in Message #1025188
|