Share via

Find Mac address

Dani_S 5,581 Reputation points
2024-11-28T07:32:51.2+00:00

Hi,

I used this code to find mac address,but if there is virtual adapter there is no mac addtess

and function return me null, how is can be fix ?

I'm in net 8,using MAUI.

/// <summary>  

/// Get MAC address  

/// </summary>  

/// <returns></returns>  

public static string GetMacAddress()  

{      

var macAddr =       (   from nic in NetworkInterface.GetAllNetworkInterfaces()

                             where nic.OperationalStatus == OperationalStatus.Up          

                             select nic.GetPhysicalAddress().ToString()       ).FirstOrDefault();

 

      if (!string.IsNullOrEmpty(macAddr))

      {  

         return macAddr;     

  } 

      return null;

  }

Developer technologies | .NET | .NET Multi-platform App UI
Developer technologies | .NET | Other
0 comments No comments

Answer accepted by question author

Trivedi, Tejas 80 Reputation points
2024-11-28T09:19:51.37+00:00

You can filter the virtual adaptor and skip it as there's no mac address associated with it.

Below is the quick code snippet.

 var adapters = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in adapters)
        {
            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
                adapter.NetworkInterfaceType == NetworkInterfaceType.Tunnel)
            {
                Console.WriteLine("Virtual Adapter: " + adapter.Description);
            }
        }

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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