title: get MAC address of computer
body: <p>I want to accesss the mac address of the computer using c#. I have used the following code to access mac address but there is some issue in this code. </p>
  
  <p><strong>Code 1</strong></p>
  
  <pre><code>foreach( NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces() )
  {
      if( nic.OperationalStatus == OperationalStatus.Up )
      {
          Console.WriteLine( nic.GetPhysicalAddress().ToString() );
          checkMAC = nic.GetPhysicalAddress().ToString();
          break;
      }
  }
  </code></pre>
  
  <p><strong>Code 2</strong></p>
  
  <pre><code> 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
          {
              if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
          }
          mo.Dispose();
      }
  
      MACAddress = MACAddress.Replace(":", "");
  </code></pre>
  
  <p>In first code when we disconnected from network connection then it will return null mac address . second code return the mac address when network adapter connection id disconnected But when we disbled the network connection or remove the IP address of the computer then it will return the null mac address .</p>
  
  <p>How to get the mac address when network connnection disabled, there is no IP address assigned to the PC or network connection disconnected?</p>