2

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 ?

Shailesh Jaiswal
  • 3,606
  • 13
  • 73
  • 124

2 Answers2

4

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
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 1
    What is not working, can you be more specific?? What do you get? Do you get an exception?? – Aliostad Oct 22 '10 at 09:49
  • In both the cases I am getting the the MAC address of the Loopback Adapter named as 'Network B'. I have one 'Real Lan Card' which is my actual Lan Card. I want the MAC Address of 'Real Lan Card' – Shailesh Jaiswal Oct 22 '10 at 10:05
  • Review your code my friend. There is got to be something else. – Aliostad Oct 22 '10 at 10:08
0

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

Community
  • 1
  • 1
weismat
  • 7,195
  • 3
  • 43
  • 58