11

Is there any way i can retrieve MAC Address when Network Adapter is disabled in .net?

Thanks in advance,

Magnus Johansson
  • 26,737
  • 17
  • 96
  • 157
Ulhas Tuscano
  • 5,224
  • 14
  • 52
  • 85
  • 2
    could you post the code you use for when the adapter is enabled, and then mention which call(s) don't work when the adapter is disabled? – James Manning Jun 30 '10 at 05:40

4 Answers4

10

It is not possible to get the MAC address of an adapter which is disabled: this is because getting the MAC address requires querying the driver, and the driver for a disabled adapter is not loaded (source).

You can, however get the MAC address of an adapter which is not currently connected.

The WMI route is no good here, because it shows the MAC address as null for adapters which are not connected. The good news is that the NetworkInterface.GetAllNetworkInterfaces() route works just fine:

// using System.Net.NetworkInformation;
var nics = NetworkInterface.GetAllNetworkInterfaces();

// pick your NIC!
var selectedNic = nics.First();

var macAddress = selectedNic.GetPhysicalAddress().ToString();
Samuel Jack
  • 30,864
  • 14
  • 113
  • 152
  • Above answer will not work bcs. returned list of network adapters is not in any sequential order. It reads all adapters and can be listed in random order. Therefore, connected and disconnected adapter can have in any index. – Navin Pandit Jul 20 '18 at 10:17
1

You can use WMI:

public static string GetMACAddress()
    {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MACAddress=String.Empty;
        foreach(ManagementObject mo in moc)
        {
            if(MACAddress==String.Empty)  // only return MAC Address from first card
            {
                MACAddress= mo["MacAddress"].ToString() ;
            }
            mo.Dispose();
        }

        return MACAddress;
    }
SqlRyan
  • 30,939
  • 32
  • 109
  • 190
Michael Stoll
  • 1,302
  • 3
  • 13
  • 32
1

Refer this link.

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

The example here displays physical address of all interface irrespective of their operational stage. HTH.

lazynewbie
  • 19
  • 1
0

Using MS PowerShell command get-NetAdapter one can get the disabled network adapter's MAC Address.

More info at get-NetAdapter