0

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:

enter image description here

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.

NoChance
  • 5,632
  • 4
  • 31
  • 45
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • Try `string.IsNullOrWhiteSpace()`? Because it could be one (or more) space. – Chi Chan Feb 21 '14 at 21:30
  • You could just set a breakpoint and inspect the value. That said, is it possible there are no items in `mObject`? Is it possible that there _are_ values but none of the `PNPDeviceID`s contain `"PCI\\"`? – canon Feb 21 '14 at 21:30
  • @ChiChan I have tried that as well -- no throw. @canon, let me try `mObject` – theGreenCabbage Feb 21 '14 at 21:32
  • Thinking about it, shouldn't `string.Empty` be returned instead? – theGreenCabbage Feb 21 '14 at 21:33
  • Some hex values may appear as empty/blank/spaces on console. When all else fails, try to display the hex value returned using ascii to hex converter function as in: http://stackoverflow.com/questions/15920741/convert-from-string-ascii-to-string-hex – NoChance Feb 21 '14 at 21:38
  • Hm.. But IsNullOrWhiteSpace should already handle all of \n\r\v\t: http://www.dotnetperls.com/isnullorwhitespace. Could it be somewhere else in your code? – Chi Chan Feb 21 '14 at 21:42
  • Hi @ChiChan is `IsNullOrWhiteSpace` better or `IsNullOrEmpty`? – theGreenCabbage Feb 21 '14 at 22:02
  • 1
    @theGreenCabbage, it depends on usage. But imagine if you want to check for an empty user input and it's a textarea. The person can type new line characters, spaces and tabs and it's very annoying to check for all of those. In that case you want to use `IsNullOrWhiteSpace`. – Chi Chan Feb 22 '14 at 18:54

2 Answers2

2

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: ...

Could it be that after disabling the card, the following condition is always false

if (pnp.Contains("PCI\\"))

In such case nothing happens within foreach loop and function returns string.Empty?

AlexD
  • 32,156
  • 3
  • 71
  • 65
0

you can use .Lenght to be sure

if (string.IsNullOrEmpty(mac) || mac.Trim().Length == 0)
{
    throw new System.Exception("Invalid PCI MAC");
}
Maryam Arshi
  • 1,974
  • 1
  • 19
  • 33