how can i get MAC Address in C#?

Member Avatar for nicentral

Use the Management Class object in .NET.

ManagementClass oMClass = new ManagementClass ("Win32_NetworkAdapterConfiguration");

ManagementObjectCollection colMObj = oMCLass.GetInstances();

foreach(ManagementObject objMO in colMObj)
{
Console.WriteLine(objMO["MacAddress"].ToString());
}

Makes sense?

Andy

Member Avatar for nicentral

Use the Management Class object in .NET.

ManagementClass oMClass = new ManagementClass ("Win32_NetworkAdapterConfiguration");

ManagementObjectCollection colMObj = oMCLass.GetInstances();

foreach(ManagementObject objMO in colMObj)
{
Console.WriteLine(objMO["MacAddress"].ToString());
}

Makes sense?

Andy

You might want to also add

Console.WriteLine(objMO["Caption"].ToString());

to your test before the mac address so that you know which devices you are looking at.

Also, don't forget to use try/catch blocks on these as a NullPointerReference will kill the app.

Andy

System.Net.NetworkInformation.NetworkInterface.GetPhysicalAddress();
or
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); and iterate through each interface, getting the MAC address for each one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.