Shailesh Jaiswal
Shailesh Jaiswal

Reputation: 3654

How to identify the MAC address of primary physical lan card?

I am developing window application in C#. I am using the following code to obtain the MAC address

private void Form1_Load(object sender, EventArgs e)
        {
            lbl1.Text = "Hi";

            string macAddresses = "";

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.OperationalStatus == OperationalStatus.Up)
                {
                    macAddresses += nic.GetPhysicalAddress().ToString();
                    break;
                }
            }
            lbl1.Text = macAddresses;

        }

In the above code I am not getting the MAC address of the primary lan card. In my computer I created two loopback adapters A & B. I have one physical Lan Card. Now I want to obtain the MAC address of the primary physical Lan Card instead of A & B. How to do this ? Can you please provide me any code or link through which I can resolve the above issue ?

Upvotes: 2

Views: 8605

Answers (2)

Aliostad
Aliostad

Reputation: 81660

Change the condition to:

 // instead of nic.OperationalStatus == OperationalStatus.Up
 nic.NetworkInterfaceType != NetworkInterfaceType.Loopback

Or use this:

    nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType  ==  etworkInterfaceType.FastEthernetFx || nic.NetworkInterfaceType ==              NetworkInterfaceType.FastEthernetT

Upvotes: 4

weismat
weismat

Reputation: 7411

I guess you could use this link if you use localhost as the target IP address...
How to get the Mac address

Upvotes: 0

Related Questions