I would like to get ONLY the built-in MAC Addresses from Laptops / Desktops, except other adapters (for Virtual Machines, VPNs, other adapters). The other adapters should be not listed!
I come up with this code:
static void Main(string[] args)
{
string mac = "0";
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(@"root\CIMV2",
string.Format(@"SELECT * FROM Win32_NetworkAdapter WHERE NOT PNPDeviceID LIKE 'ROOT\\%'")))
{
bool existingnic = false;
foreach (ManagementObject share in searcher.Get())
{
if (share.Properties["AdapterType"].Value != null &&
share.Properties["AdapterType"].Value.Equals("Ethernet 802.3") &&
share.Properties["MACAddress"].Value != null
&& !share.Properties["MACAddress"].Value.Equals(String.Empty))
{
if (Convert.ToBoolean(share.Properties["PhysicalAdapter"].Value))
{
mac = (Int64.Parse(mac.ToString(), System.Globalization.NumberStyles.HexNumber)
+ Int64.Parse(share.Properties["MACAddress"].Value.ToString().Replace(":",""), System.Globalization.NumberStyles.HexNumber)).ToString();
Console.WriteLine(share.Properties["MACAddress"].Value.ToString().Replace(":", ""));
//Console.WriteLine(Int64.Parse(share.Properties["MACAddress"].Value.ToString().Replace(":", ""), System.Globalization.NumberStyles.HexNumber).ToString());
existingnic = true;
break;
}
}
}
if(!existingnic)
foreach (ManagementObject share in searcher.Get())
{
if (share.Properties["AdapterType"].Value != null &&
share.Properties["AdapterType"].Value.Equals("Wireless") &&
share.Properties["MACAddress"].Value != null
&& !share.Properties["MACAddress"].Value.Equals(String.Empty))
{
if (Convert.ToBoolean(share.Properties["PhysicalAdapter"].Value))
{
mac = (Int64.Parse(mac.ToString(), System.Globalization.NumberStyles.HexNumber)
+ Int64.Parse(share.Properties["MACAddress"].Value.ToString().Replace(":", ""), System.Globalization.NumberStyles.HexNumber)).ToString();
Console.WriteLine(share.Properties["MACAddress"].Value.ToString().Replace(":", ""));
//Console.WriteLine(Int64.Parse(share.Properties["MACAddress"].Value.ToString().Replace(":", ""), System.Globalization.NumberStyles.HexNumber).ToString());
// break;
}
}
}
}
string myHex = IntToHexString(Convert.ToInt64(mac), 12);
Console.WriteLine(myHex);
Console.ReadLine();
}
Thanks
I tried to use the above code.
Note: I found a list with MAC Addresses that identifies a company / organization, but I don't know which to include and which not. The list I found: ieee.org