Home All Groups Group Topic Archive Search About

testing internet connectin

Author
18 Mar 2006 2:57 PM
Rick
>From a C# windows app, how can I easily tell if I have a connection to
the internet.

Thanks,
Rick

Author
18 Mar 2006 3:13 PM
PiotrKolodziej
Easiest way is to try connect somewhere and catch exception if needed.
Are all your drivers up to date? click for free checkup

Author
18 Mar 2006 3:15 PM
Vadym Stetsyak
Hello, Rick!

You can try to connect to a well known internet resource.
try
{
    TcpClient tcpClient = new TcpClient ();
    tcpClient.Connect ("www.microsoft.com", 80);
    //connection is available
}
catch(SocketException ex)
{
    //smth is wrong - maybe there is no connection
}

The approach above is rather straightforward, can you tell why do you have to know if you're connected to the Internet.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Author
19 Mar 2006 12:12 AM
Rick
I prefer not to have to catch exceptions, but this will work.
(I am retrieving information from the web or a local cache if not
connected.)

Thanks,
Rick
Author
19 Mar 2006 9:25 AM
Jocker
you may try this:

[System.Runtime.InteropServices.DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int lpdwFlags,
int dwReserved) ;

public static bool IsConnectedToTheInternet( )
{
   int flags;
   return InternetGetConnectedState(out flags, 0);
}


Bookmark and Share