I am developing an windows application in that i want to check internet connection for that i am written the following code but it seems to be not working .

Can anybody help me......

        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

        public static bool IsConnectedToInternet()
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
7 Years
Discussion Span
Last Post by Momerath

The InternetGetConnectedState actually only tells you if you're connected to a network.

What we do here, is pick a website and attempt to ping it. As you're wanting internet connectivity I presume there is a target machine somewhere that you're looking for. Try pinging that, if not, try google?

Ping test = new Ping();
PingReply result = test.Send("google.com");
if (result.Status == IPStatus.Success) {
    // there is a network connection between the machines

You'll need a using System.Net.NetworkInformation at the top of your code.

Edited by Momerath: n/a


Hi Momerath Thanks for you reply.

it works Fine when connection is available but when connection is not available then it throws an exception error for ping. I will find it by using Try...Catch.. is it ok or some another way....

Edited by bhagawatshinde: n/a


The error you are getting comes from the system trying to look up "google.com" in the DNS and not being able to find it. Your suggested solution (try/catch) is the best one.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.