tags:

views:

29

answers:

3

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 ?

A: 

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

weismat
+3  A: 

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
Its not working
Shailesh Jaiswal
Updated the answer
Aliostad
It is also not working
Shailesh Jaiswal
What is not working, can you be more specific?? What do you get? Do you get an exception??
Aliostad
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
Review your code my friend. There is got to be something else.
Aliostad