I am currently using two methods on my Windows 7 Bootcamp (running on a Macbook Pro) to retrieve a MAC address. I am currently investigating some issues with people running my C#-written application on a virtualized machine, where they were unable to get authenticated from my MAC address authentication mechanism. I don't have a VM build on hand, so I chose the closest alternative - my boot camp.
The first method retrieves a MAC address based on the fact that the physical card is connected to the PCI interface.
public static string returnMAC1()
{
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);
return mac;
}
}
return "Nothing happened...";
}
The second method is a standard method from the MSDN documentation:
public static string returnMAC2()
{
string mac = string.Empty;
foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return mac;
}
Using both methods, I get two entirely different results:
Here is what gets printed in my two methods:
When I check out the MAC address from the Network and Sharing Center
-> Wireless Network Connection
, it corresponds to method 2
's result.
Does the fact that I get two entirely different results have to do with the fact that method #1 is giving me the result of not the wireless card's MAC, but the actual PCI card's MAC? That seems to be the most simple of an explanation, but possible.