Ashutosh Tripathi

Ashutosh Tripathi

  • NA
  • 44
  • 11.6k

How to access the MAC address of the system?

Aug 25 2015 12:56 PM
How to access the MAC address of the system?
 
I have a code which is working fine but only on localhost. After publishing my website on server, it cannot access the MAC Address and either throw an exception object reference not set to an instance of an object  or after using try catch it move to catch block.
 
I am providing my code, so that anyone can suggest me something fruitful:
 
public string GetMACAddress()
{
try
{
ManagementClass objMOS = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMOS.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject objMO in objMOC)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
MACAddress = objMO["MacAddress"].ToString();
}
objMO.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
catch (Exception ei)
{
return "No Mac";
}
}
  

Answers (9)