Need to acquire the MAC of the machine running this app.

A

AMeador

I have searched for the answer to this in many sources, but have not
found a solution that works. I'm wondering if maybe I am missing a
reference, or something. I added a reference to the project by going to
Solution Explorer...Under Project Name...Right Clicked on
Reference...Clicked on Add Reference...Selected the .NET tab...Selected
System.Management...Clicked OK. Here is the code I have tried and the
resulting errors:

using System;
using System.Management;
// I use the same using statements above for each sample below
..
..

// Set up the query.
ManagementObjectSearcher pobjSearcher = new
ManagementObjectSearcher("sele­ct
MACAddress from Win32_NetworkAdapter");

// Perform the query and get the addresses.
foreach (ManagementObject pobjObject in pobjSearcher.Get())
// Print the MAC Address to the console.
Console.WriteLine(pobjObject.I­tem["MACAddress"]);

When I try to compile this, I get the following error:
C:\Development\Work\DoEA Test App\DoEA Test App\frmTest.cs(175):
'System.Management.ManagementObject' does not contain a definition for
'Item'


This is another sample I found. This compiles with no errors, but when
I run it, I get the error posted below the code.

ManagementClass mc = new
ManagementClass("Win32_Network­AdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address\t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}

An unhandled exception of type 'System.Management.ManagementException'
occurred in system.management.dll Additional information: Not found


The line highlighted as the error is:
ManagementObjectCollection moc = mc.GetInstances();


Thanks in advance for any help you can give!

- Andrew
 
D

Dmytro Lapshyn [MVP]

Hi,

Try it this way (based on your second example):

foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address\t{0}", mo["MACAddress"].ToString());
mo.Dispose();
}

Mind the case-sensitiveness of the MACAddress property name.

* OR *

in your first example, change this line:

Console.WriteLine(pobjObject.I­tem["MACAddress"]);

to

Console.WriteLine(pobjObject["MACAddress"]);

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


I have searched for the answer to this in many sources, but have not
found a solution that works. I'm wondering if maybe I am missing a
reference, or something. I added a reference to the project by going to
Solution Explorer...Under Project Name...Right Clicked on
Reference...Clicked on Add Reference...Selected the .NET tab...Selected
System.Management...Clicked OK. Here is the code I have tried and the
resulting errors:

using System;
using System.Management;
// I use the same using statements above for each sample below
..
..

// Set up the query.
ManagementObjectSearcher pobjSearcher = new
ManagementObjectSearcher("sele­ct
MACAddress from Win32_NetworkAdapter");

// Perform the query and get the addresses.
foreach (ManagementObject pobjObject in pobjSearcher.Get())
// Print the MAC Address to the console.
Console.WriteLine(pobjObject.I­tem["MACAddress"]);

When I try to compile this, I get the following error:
C:\Development\Work\DoEA Test App\DoEA Test App\frmTest.cs(175):
'System.Management.ManagementObject' does not contain a definition for
'Item'


This is another sample I found. This compiles with no errors, but when
I run it, I get the error posted below the code.

ManagementClass mc = new
ManagementClass("Win32_Network­AdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address\t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}

An unhandled exception of type 'System.Management.ManagementException'
occurred in system.management.dll Additional information: Not found


The line highlighted as the error is:
ManagementObjectCollection moc = mc.GetInstances();


Thanks in advance for any help you can give!

- Andrew
 
A

AMeador

Thanks for the help. The second sample I gave was only different by
the case of MACAddress from what I can see. I made this change, but I
still get the same error as before. With the modifications to the first
sample I gave - it worked!!! Yeahh!
Here is the code as it is now:

string MAC = "";
lstMACs.Items.Clear();
// Set up the query.
ManagementObjectSearcher pobjSearcher = new
ManagementObjectSearcher("select MACAddress from
Win32_NetworkAdapter");

// Perform the query and get the addresses.
foreach (ManagementObject pobjObject in pobjSearcher.Get())
{
MAC = (string)pobjObject["MACAddress"];
if (MAC != null)
lstMACs.Items.Add(pobjObject["MACAddress"]);
}

I found that some of the objects were null, and that was what was
breaking my code before, so now I check for a null before adding it to
my listbox.
Now, I need to figure out what all the MACs are that are returned. I
am getting 9 unique MACs returned from my laptop (it has 3 adaptors -
10/100 ethernet, G-Wireless, and Bluetooth. That leaves six to figure
out. Plus, they are returned one, two, and three times - I mean the
same MAC is listed multiple times in the result. But this is definitely
a step in the right direction.
I would still be interested in understanding what's wrong with the
second code sample though. If you have any other ideas, please let me
know.

--- Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top