.NET: Using WMI to get MACAddress of a machine

using System; 
using System.Management; 
namespace GetMACAddress 
{ 
class Class1 
{ 
 [STAThread] 
 static void Main(string[] args) 
 { 
  ManagementObjectSearcher query = null; 
  ManagementObjectCollection queryCollection = null; 
  try 
  { 
   query = new ManagementObjectSearcher(new ObjectQuery 
("Select MacAddress,IPAddress from Win32_NetworkAdapterConfiguration where 
IPEnabled=TRUE")) ; 
   queryCollection = query.Get(); 
   foreach( ManagementObject mo in queryCollection ) 
   { 
    if(mo["IPAddress"] != null && mo 
["MACAddress"] != null) 
    { 
     Console.WriteLine("IPAddress : " + 
((String[])mo["IPAddress"])[0]); 
     Console.WriteLine("MACAddress : " + 
mo["MACAddress"].ToString()); 
    } 
   } 
  } 
  catch(Exception ex) 
  { 
   Console.WriteLine(ex.Source); 
   Console.WriteLine(ex.Message); 
  } 
 } 
} 
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: