Mac Address

anil kumar 61 Reputation points
2022-01-28T09:54:34.367+00:00

I am working with a wpf application in which i am generating mac address and store it in database.It's working fine but when my system connect with wifi it gives another and when it is connected with ethernet it gives difference .Now problem create when I get it from database and match. it create problem because of it was saved with ethernet.
But now my system is connected with wifi and it gives wifi mac address.How to comes out from this .
Please give proper solution.
I am using below code for the same.
Thanks & Regards
public string GetMACAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

         string  sMacAddress="";
        foreach (NetworkInterface adapter in nics)
        {
           // if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet || adapter.NetworkInterfaceType == //NetworkInterfaceType.Wireless80211) && adapter.OperationalStatus == OperationalStatus.Up)
            {

                    IPInterfaceProperties properties = adapter.GetIPProperties();
                  sMacAddress= adapter.GetPhysicalAddress().ToString();

            }
        }
        return sMacAddress;

    }
Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2022-01-29T22:38:04.75+00:00

    You need to get mac address like this. BitConverter.ToString(adapter.GetPhysicalAddress().GetAddressBytes())

        public string GetMACAddress()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    
            string sMacAddress = "";
            foreach (NetworkInterface adapter in nics)
            {
                if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet || adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && adapter.OperationalStatus == OperationalStatus.Up)
                {
    
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    sMacAddress = BitConverter.ToString(adapter.GetPhysicalAddress().GetAddressBytes());
    
                }
            }
            return sMacAddress;
    
        }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.