Reputation: 2735
I need a way to retrieve the MAC
address of a machine with Windows 7
when its Network Interface Card
is disabled and using C#
. I searched on the web and also refereed to the following links. Using the answer in the 2nd link I could get the required details successfully in Windows XP
but not in Windows 7
when NIC
is disabled.
Get MAC Address when network adapter is disabled?
Does anyone know how to get this task done???
Thanks...
Upvotes: 2
Views: 3404
Reputation: 9492
I think you need to cache the MAC address. For example, when the network card is enabled you can update the MAC address in the cache for that card. Later, when it's disabled and you can't get the MAC from Windows, you can get it from your cache.
You can't get the MAC address from a driver that isn't even loaded. And the driver is required to load the MAC address from the network card's ROM chip. So caching the MAC would have to be the same technique Windows would use, if Windows did have a way to get a MAC address from a disabled network card.
Upvotes: 1
Reputation: 11618
Here's what I came up with (Win7 64 bit):
var query = new SelectQuery("Win32_NetworkAdapter");
var scope = new ManagementScope("\\root\\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher(scope, query);
var managementObjects = searcher.Get();
foreach (var mo in managementObjects)
{
Debug.WriteLine("{0} : {1}", mo["Description"], mo["MACAddress"]);
}
My bluetooth adapter looks like this:
Bluetooth Device (Personal Area Network) : 70:F3:95:88:F7:7E
However, when its disabled, the MAC Address shows up as blank.
You should be able to Enable/Disable the adapters to query the MAC via methods on the class, but its a bit of a chore as you manually have to wrap the Win32_NetworkAdapter class.
You need to generate a class wrapper for the WMI object using the .Net Framework SDK tool 'mgmtclassgen.exe'
Invoke it like this (the generated file is 80k):
mgmtclassgen Win32_NetworkAdapter -p NetworkAdapter.cs
Then augment the code like so:
var query = new SelectQuery("Win32_NetworkAdapter");
var scope = new ManagementScope("\\root\\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher(scope, query);
var managementObjects = searcher.Get();
var adapters = managementObjects.Cast<ManagementBaseObject>().Select(s => new NetworkAdapter(s));
foreach (var adapter in adapters)
{
adapter.Enable();
Console.WriteLine("{0} : {1}", adapter.Name, adapter.MACAddress);
}
But I couldn't get it to work as nothing happened when I called Enable() and the return code was 0. I posted it in the hopes that you or someone might deduce the missing detail that will allow it to work.
Upvotes: 0