■ ManagementClass 클래스를 사용해 MAC 주소를 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System.Management; #region MAC 주소 구하기 - GetMACAddress() /// <summary> /// MAC 주소 구하기 /// </summary> /// <returns>MAC 주소</returns> public string GetMACAddress() { ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection managementObjectCollection = managementClass.GetInstances(); string macAddress = string.Empty; foreach(ManagementObject managementObject in managementObjectCollection) { if(macAddress == string.Empty) { if((bool)managementObject["IPEnabled"] == true) { macAddress = managementObject["MacAddress"].ToString(); } } managementObject.Dispose(); } return macAddress.Replace(":", "-"); } #endregion |