Thursday, September 16, 2004

.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);
}
}
}
}

No comments: