1using System;
2// Be sure to add a reference to System.Management for this class to work properly
3using System.Management;
4
5namespace MacAddress
6{
7 class MACAddress
8 {
9 public string GetMACAddress()
10 {
11 return GetMACAddress("");
12 }
13
14 public string GetMACAddress(string separator)
15 {
16 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
17 ManagementObjectCollection moc = mc.GetInstances();
18 string MACAddress = String.Empty;
19 foreach (ManagementObject mo in moc)
20 {
21 if (MACAddress == String.Empty) // only return MAC Address from first card
22 {
23 if ((bool)mo["IPEnabled"] == true)
24 MACAddress = mo["MacAddress"].ToString();
25 }
26 mo.Dispose();
27 }
28 MACAddress = MACAddress.Replace(":", separator);
29 return MACAddress;
30 }
31 }
32}
33