Accessing Network Card Information in C#

It is common for modern PCs to have more than one NIC, including virtual ones (such as those created by VMware for example). This tutorial will talk you through how to access each card’s information.

First of all go ahead and add  System.Net.NetworkInformation namespace declaration. This gives us access to a lot of fun things including the NetworkInterface objects we need for this example.

Next we create an array of NetworkInterface objects by calling the GetAllNetworkInterfaces method.

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

We can then iterate through this array to extract the relevant information needed. In this example I insert the information from each card into a datagridview.  So first of all create a Form and drag a dataGridView onto it. Then initialise the grid view inside the constructor like so:

dataGridView1.Columns.Add(“MAC”, “Mac Address”);
dataGridView1.Columns.Add(“Type”, “Type”);
dataGridView1.Columns.Add(“Name”, “Name”);

Now you can loop through the array and populate your dataGridView with the information:

foreach (NetworkInterface adapter in nics){

string[] mac = { adapter.GetPhysicalAddress().ToString(), adapter.NetworkInterfaceType.ToString(), adapter.Description };
dataGridView1.Rows.Add(mac);

for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
this.dataGridView1.AutoResizeRow(i);
}
}

Download the complete source here

Leave a comment