Add the reference System.Management and Import System.Management namespace.
Get the Mac address of all network cards in the machine
Get the Mac address of all network cards in the machine
protected void GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = "";
foreach (ManagementObject mo in moc)
{
if (mo["MacAddress"] != null)
{
MACAddress = mo["MacAddress"].ToString();
MessageBox.Show(MACAddress);
}
}
}
Get the Mac address of network card is being used in the machine
protected void GetMACAddress(){ ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); string MACAddress = ""; foreach (ManagementObject mo in moc) { if (mo["MacAddress"] != null) { if ((bool)mo["IPEnabled"] == true) { MACAddress = mo["MacAddress"].ToString(); MessageBox.Show(MACAddress); } } } }
Output: