Get Mac Address

Dani_S 4,501 Reputation points
2024-02-18T12:37:49.0133333+00:00

Hi, How do I get Mac address ? Thanks in adavance,

Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-02-20T08:51:48.2033333+00:00

    Hi @Dani_S ,

    The code you provided retrieves the MAC (Media Access Control) address of the network interface that is currently operational. It utilizes the NetworkInterface.GetAllNetworkInterfaces() method to get all network interfaces available on the system, then filters them using a LINQ query to select only those interfaces with OperationalStatus equal to OperationalStatus.Up, indicating they are operational and can transmit data.

    Once it finds the first matching interface, it retrieves its physical address using nic.GetPhysicalAddress(), converts it to a string, and returns it. If no matching interface is found or if the MAC address is empty or null, it returns null.

    This function could be used in scenarios where you need to identify the MAC address of the primary network interface for network-related tasks or for device identification purposes.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2024-02-18T12:39:21.9666667+00:00

    Assuming you are using C#, the following should work:

    using System;
    using System.Net.NetworkInformation;
    class Program
    {
        static void Main()
        {
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                Console.WriteLine($"Interface: {networkInterface.Description}");
                Console.WriteLine($"  MAC Address: {networkInterface.GetPhysicalAddress()}");
                Console.WriteLine();
            }
        }
    }
    
    

    hth Marcin


  2. webserverforum 65 Reputation points
    2024-02-18T15:29:29.3133333+00:00

    exactly how do you want to get Mac address ? are you checking for it on any machine/PC/server or you want to get it from code ? Best Regards, https://www.webserverforum.com/

    0 comments No comments

  3. webserverforum 65 Reputation points
    2024-02-18T15:32:06.2233333+00:00

    if you want to get Mac address on a PC then you can run following command on command prompt : getmac it will show the Mac addresses Regards, https://www.webserverforum.com/


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.