Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MAC ID of a system using C#

Tags:

c#

.net

I am building a C# application and I want to fetch the MAC ID of the system. I have found many code snippets, but they either give wrong answers or throw exceptions. I am not sure which code snippet is giving the right answer. Can someone provide me the exact code snippet that fetches the MAC ID?

like image 331
sumit_programmer Avatar asked Aug 30 '10 06:08

sumit_programmer


People also ask

How do I find my MAC address Linux C?

the file /sys/class/net/eth0/address carries your mac adress as simple string you can read with fopen() / fscanf() / fclose() . Nothing easier than that. And if you want to support other network interfaces than eth0 (and you probably want), then simply use opendir() / readdir() / closedir() on /sys/class/net/ .

Can you get MAC address from UUID?

The last 12 hex digits of a UUID string represent the MAC address of the node. In some implementations (including the UUID generator on this site) a random MAC address is used instead of the node's actual MAC. MAC Address and creation time can be extracted using our UUID decode tool.


1 Answers

This will help you.

public string FetchMacId()
{
    string macAddresses = "";

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }
    return macAddresses;
}
like image 63
Pankaj Mishra Avatar answered Sep 28 '22 16:09

Pankaj Mishra