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);

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?

can you provide some source code for ping 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.

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....

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.