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?
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/ .
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With