Friday, December 3, 2010

List All Network Adapters and Show Further Information

As in the previous post we again use the class System.Net.NetworkInformation here. The function  NetworkInterface.GetAllNetworkInterfaces() returns an array of the type System.Net.NetworkInformation.NetworkInterface, in which each entry represents a network interface or network adapter.
We now can iterate over this array and list all network adapters in this way. The following example prints name and status (is explained further in the next post) of each network adapter:

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())
            {
                Console.WriteLine("Name: " + n.Name + " Status: " + n.OperationalStatus.ToString());
            }
        }
    }
}

No comments:

Post a Comment