This is the method I use to retrieve MAC address from my user.
To reproduce a scenario where a MAC address does not get retrieved, I intentionally disabled my network card. This should not return a MAC address, using this method:
public static string returnMAC1()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select MACAddress, PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDEVICEID IS NOT NULL");
ManagementObjectCollection mObject = searcher.Get();
foreach (ManagementObject obj in mObject)
{
string pnp = obj["PNPDeviceID"].ToString();
if (pnp.Contains("PCI\\"))
{
string mac = obj["MACAddress"].ToString();
mac = mac.Replace(":", string.Empty);
if (string.IsNullOrEmpty(mac))
{
throw new System.Exception("Invalid PCI MAC");
}
return mac;
}
}
return string.Empty;
}
catch (Exception ex)
{
Console.WriteLine("Error Message: " + ex.Message);
}
return string.Empty;
}
With this said, despite my exception handler in place to throw an error, I am unable to get it to throw an error despite my output being like this:
Note: It should say: Error Message: Invalid PCI MAC
.
Is there any way to check what that "blank" is? Apparently it's not a ""
, or string.Empty
, or null
, or null
.