retrieving MAC address using C# question.

G

Guest

Hi,

This is the code I'm using to get MAC address..
-------------------------------------------------------------------------------------------
ManagementClass mc = new ManagementClass

("WIN32_NetworkAdapterConfiguration");
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();
}
}
-----------------------------------------------------------------------------------------------

Execution of this code is taking about 4-5 seconds!!!!. Is this normal or
am I doing anything wrong.. is there any quicker way of finding MAC address
of
of a PC using C#.

Kindly let me know

Cheers,

Naveen.
 
W

Wessel Troost

("WIN32_NetworkAdapterConfiguration");

Try "Win32_NetworkAdapter"?

Since that retrieves less information, it might be faster. On my PC both
are reasonably fast.

Greetings,
Wessel
 
W

Willy Denoyette [MVP]

Naveen Mukkelli said:
Hi,

This is the code I'm using to get MAC address..
-------------------------------------------------------------------------------------------
ManagementClass mc = new ManagementClass

("WIN32_NetworkAdapterConfiguration");
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();
}
}
-----------------------------------------------------------------------------------------------

Execution of this code is taking about 4-5 seconds!!!!. Is this normal or
am I doing anything wrong.. is there any quicker way of finding MAC
address
of
of a PC using C#.

Kindly let me know

Cheers,

Naveen.

No it's not normal, this should take less than a second.
Try running wbemtest.exe; connect to the root\cimv2 namespace and execute
the same query.
If this also takes 4-5 seconds you have some environmental problem like CPU
resources, memory resources etc.
What OS are you running this on?

Willy.
 
H

Hesham Hafez

hi all i'm kinda new using c# so when i tried the above code to get mac
address but with addition that i wanted the mac addressbe displayed in a
form textbox actually nothing showed i wounder if any body can point me
to what is wrong with this code or any other code to make the return
displayed in a textbox

ManagementClass mc = new ManagementClass
("WIN32_NetworkAdapterConfiguration");
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();
} this.textBox1.Text=MACAddress.Tostring();
}
 

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