Alex Rivera | Logout

What's the easiest way to verify there's an available network connection?

Asked 2009-02-03T16:33:42.773
9

I'm a bit of newbie to c#/.net development, but I've put together a stock tracking application for a small set of assets in my company. I have also set up the database it connects to in SQL 2000.

It currently works brilliantly when a network connection is available, but I want to expand it for use when I'm away from a connection.

First off I'll need to know if there's a connection available. So I put this together:

    private int availableNetAdapters()
    {
        int nicCount = 0;
        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                nicCount++;
            }
        }

        return nicCount;
    }

Seems to work, but I have to test for ">1" as something as "MS TCP Loopback interface" is always detected regardless of the other adapter sates.

Is there a better/easier way to check connectivity?

G

Edit
Report

1 Answer

8

Some more things to remember:

  • Avaialable Network connection != Available internet connection.
  • Internet Access != access to a specific web site (think proxy filters, or the site could just be down)

Therefore it's generally best to test for access to the specific resource you need directly.

These resources are volatile; you have to handle the exception when they go down anyway. So it's generally best to just go get a resource as if you know for sure it exists and put your development time in to making sure your exception handler does a nice job with the failures.

answered 2009-02-03T16:51:55.217

Your Answer