2010年4月14日 星期三

獲得mac address

using System;
using System.Management;

class App
{
  static string GetMAC()
  {
 using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
 {
   ManagementObjectCollection moc = mc.GetInstances();
   foreach (ManagementObject mo in moc)
   {                
     if ((bool)mo["IPEnabled"])
       return mo["MacAddress"].ToString();
   }
 }
   return null;
  }

  public static void Main()
  {
 SelectQuery query = new SelectQuery("Win32_ComputerSystem");
 ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
 foreach (ManagementObject mo in searcher.Get())
 { 
   Console.WriteLine("Mac Address : {0} ", GetMAC());
 }
 Console.ReadKey();
  }
}

沒有留言: