Friday, December 3, 2010

Check Status of a Network Adapter

In a previous post was shown how to check if the computer is connected with a network.
In this post now we determine the status of a network interface more exactly. We do not want to distinguish any more only between "connected" and "not connected", but get more information.
Again we use the class System.Net.NetworkInformation, but this time use the property OperationalStatus.
This can have the following values:

  • Dormant - The interface waits for an external event, it cannot transmit data.
  • Down - No connection.
  • LowerLayerDown - The interface cannot transmit data, since it user lower layer interfaces and at least one of them is not working.
  • NotPresent - Not working, mostly due to a hardware failure.
  • Testing - The interface is doing tests.
  • Unknown - Unknown status.
  • Up - The interface is working.

The following small sample lists all network adapters which are working:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (System.Net.NetworkInformation.NetworkInterface n in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
            {
                if (n.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
                    Console.WriteLine(n.Name + " is up.");
            }     
        }
    }
}

No comments:

Post a Comment