C# – Check if connected to internet

I was working on an application which checked for updates. I used WebRequest/WebResponse for checking updates. It showed a continuous Progressbar while checking for updates. But there was a problem. It worked properly when you were connected to internet but it  just showed the progressbar all the time if not connected.

The solution for that was to check for internet connection before checking for updates. If connected then check for updates otherwise show an error.

Checking for the state of internet connection is very simple. Let’s get started.

Firstly, import System.Runtime.InteropServices namespace. Add this to the top of your code:

using System.Runtime.InteropServices;

Then use this code for checking connection state:

For C# Beginners: Add this code above or below public Form1() { … }

// API Method
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

// A method for checking the state
public static bool IsConnected()
{
    int Description;
    return InternetGetConnectedState(out Description, 0);
}

Now you can use the above method to check for connection before doing anything that requires internet connection.

Example use:

// If internet connection is active
if (IsConnected())
{
    // do something
}
else
{
   MessageBox.Show("Please connect to the internet.");

If you have any questions please don’t hesitate to post a comment.

6 thoughts on “C# – Check if connected to internet

  1. Steve

    I was using webrequest/webresponse to check the internet connection but.. this way is much better, it is exactly what i was looking for.
    Thank you!

    Reply
  2. nido

    Farooq at the starting I was using the same technique as you describe in your post.
    I am sharing a simple line which will give you 100% correct out put is you connect to net or not.
    public static bool NetStatus()
    {
    return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
    }
    it is just one line function and it working great, I also like your post. Good work and keep posting.

    Reply
  3. Jagadeesan Durairaj

    I think farooq’s method is easy and accurate, because that code doesnt get the accurate result. It says connected to internet even if im not connected to it.

    Reply
  4. Jagadeesan Durairaj

    Sorry not farooq, Nido…. Nido’s code is correct and accurate whereas farooq’s code is defective…

    Reply
  5. Mouse

    Farooq’s method does work as it shows if a network connection is established ,however this does not necessarily mean that their is an internet connection as you may be connected to the network but cut off from the net. Therefore the System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() method is useful if your resources are on your local intranet.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>