how to retreive the MAC address ?

  • Thread starter Thread starter =?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=
  • Start date Start date
Hi Herbert,

One of the ways is to use the unmanaged Netbios() function from netapi32.dll
through P/Invoke.
You can find more information in the following MSDN article:

'Getting the MAC Address for an Ethernet Adapter' in Platform SDK: Netbios
 
Hi!
Use the code below:

System.Management.SelectQuery sq = new
System.Management.SelectQuery("Win32_NetworkAdapterConfiguration",
"IPEnabled='TRUE'");

System.Management.ManagementObjectSearcher searcher = new
System.Management.ManagementObjectSearcher(sq);

foreach (System.Management.ManagementObject mos in searcher.Get())

{

MessageBox.Show(mos.GetPropertyValue("MACAddress").ToString());

}



Hope that helps.

Best regards, Adrian.
 
Adrian said:
Hi!
Use the code below:

System.Management.SelectQuery sq = new
System.Management.SelectQuery("Win32_NetworkAdapterConfiguration",
"IPEnabled='TRUE'");

System.Management.ManagementObjectSearcher searcher = new
System.Management.ManagementObjectSearcher(sq);

foreach (System.Management.ManagementObject mos in searcher.Get())

{

MessageBox.Show(mos.GetPropertyValue("MACAddress").ToString());

}



Hope that helps.

Best regards, Adrian.


thanks, i've found another way:

ManagementObjectSearcher query = null;
ManagementObjectCollection queryCollection = null;

try
{
query = new ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration") ;

queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
if((bool)mo["IPEnabled"] == true)
{
Console.WriteLine(mo.GetText(TextFormat.Mof));
Console.WriteLine(mo["MacAddress"]);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
 
Back
Top