Sunday, February 19, 2012

Accessing Network Card Information using .Net (C#)?



In today’s computer, multiple Network card is installed on single machine. Sometimes we might need to get information like how many network cards are installed and details of each network card. Dotnet provides NetworkInterface class to get information about all network cards installed on machine. This class is available in System.Net.NetworkInformation namespace.

NetworkInterface class provides low level information about Network card installed on computer. NetworkInterface class is an abstract class and provides GetAllNetworkInterfaces method to retrieve all the Network cards installed on machine. This class gives us lots of fun things. Let’s have a look on below example.

//retrieves all network interface cards
NetworkInterface[] netInterfaces = NetworkInterface.GetAllNetworkInterfaces();
if (netInterfaces.Length < 1 || netInterfaces == null)
{
    Console.WriteLine("No Network card installed on your machine...");
    return;
}
Console.WriteLine("Total Network card installed on your machine is: {0}", netInterfaces.Length);
//loop all the NICs
foreach (NetworkInterface nic in netInterfaces)
{
    Console.WriteLine("{0}", new String('-', 30));
    Console.WriteLine("NIC ID: {0}", nic.Id);
    Console.WriteLine("Name: {0}", nic.Name);
    Console.WriteLine("Description: {0}", nic.Description);
    Console.WriteLine("Physical Address: {0}", nic.GetPhysicalAddress().ToString());
    Console.WriteLine("Interface Type: {0}", nic.NetworkInterfaceType);
    Console.WriteLine("Operational Status: {0}", nic.OperationalStatus);
    Console.WriteLine("Supports Multicast: {0}", nic.SupportsMulticast);
    Console.WriteLine("IPv4: {0}", nic.Supports(NetworkInterfaceComponent.IPv4) ? "Yes" : "No");
    Console.WriteLine("IPv6: {0}", nic.Supports(NetworkInterfaceComponent.IPv6) ? "Yes" : "No");
   
    //retrieves ip related information from NIC
    IPInterfaceProperties ipProp = nic.GetIPProperties();

    Console.WriteLine("DNS Enabled: {0}", ipProp.IsDnsEnabled);
    Console.WriteLine("DNS Suffix: {0}", ipProp.DnsSuffix);
    Console.WriteLine("Dynamically enabled DNS: {0}", ipProp.IsDynamicDnsEnabled);
}

Output –

Total Network card installed on your machine is: 3
------------------------------
NIC ID: {137F90A1-8654-4F46-6523-072636A6E415}
Name: Wireless Network Connection
Description: Intel(R) WiFi Link 1000 BGN
Physical Address: 0026C7CB31FC
Interface Type: Wireless80211
Operational Status: Up
Supports Multicast: True
IPv4: Yes
IPv6: Yes
DNS Enabled: False
DNS Suffix: broadband.mtnl.com
Dynamically enabled DNS: True
------------------------------
NIC ID: {846EE342-8654-11DE-2451-806E6F6E6963}
Name: Loopback Pseudo-Interface 1
Description: Software Loopback Interface 1
Physical Address:
Interface Type: Loopback
Operational Status: Up
Supports Multicast: True
IPv4: Yes
IPv6: Yes
DNS Enabled: False
DNS Suffix:
Dynamically enabled DNS: True
------------------------------
NIC ID: {BE831E47-9874-4EF1-5412-C087C5FEED04}
Name: Local Area Connection* 17
Description: Teredo Tunneling Pseudo-Interface
Physical Address: 00000000000000E0
Interface Type: Tunnel
Operational Status: Up
Supports Multicast: False
IPv4: No
IPv6: Yes
DNS Enabled: False
DNS Suffix:
Dynamically enabled DNS: False


See Also –


No comments:

Post a Comment