JayNaz
JayNaz

Reputation: 363

Get MAC Address using ManagementObjectSearcher

I am trying to develop a registration algorithm in C#. I used MAC address of the client machine to generate the request code. The function is shown below. But in Windows 7, This function shows a NullRererenceException in this line.

mac = mo["MACAddress"].ToString();

public string GetMACAddress()
{
      string mac = null;
      ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
      foreach (ManagementObject mo in mos.Get())
      {
           mac = mo["MACAddress"].ToString();
           break;
      }

      return mac;
}

What is the most reliable way to get MAC address, in Windows 7 and Windows 8, using C#, in order to develop an activation algorithm?

Upvotes: 1

Views: 3148

Answers (2)

Maximilian Scherer
Maximilian Scherer

Reputation: 69

For the purpose of license activation I would actually recommend to use something else (or in addition) to the MAC address, as this is easily spoofed. Here is a very nice C# tutorial on how to obtain a "hardware fingerprint" that should solve your problem: http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer

Upvotes: 0

too_cool
too_cool

Reputation: 1194

Not all object content the MAC address so need to check which one dose have the MAC

you can do some thing like this

string macAddress = String.Empty;
foreach (ManagementObject mo in mos.Get())
 {
      object tempMacAddrObj = MO["MacAddress"];

    if (tempMacAddrObj == null) //Skip objects without a MACAddress
    {
        continue;
    }
    if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address
    {
        macAddress = tempMacAddrObj.ToString();              
    }
    objMO.Dispose();
 } 

Upvotes: 1

Related Questions