2015년 5월 21일 목요일

C# Get Clinent MacAddress




  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Management;
  7. using System.Net.NetworkInformation;
  8.  
  9. namespace test
  10. {
  11. public static class MacAddress
  12. {
  13. public static string GetMacAddress()
  14. {
  15. const int MIN_MAC_ADDR_LENGTH = 12;
  16. string macAddress = string.Empty;
  17. long maxSpeed = -1;
  18.  
  19. foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  20. {
  21. string tempMac = nic.GetPhysicalAddress().ToString();
  22. if (nic.Speed > maxSpeed && !string.IsNullOrEmpty(tempMac) && tempMac.Length >= MIN_MAC_ADDR_LENGTH)
  23. {
  24. maxSpeed = nic.Speed;
  25. macAddress = tempMac;
  26. }
  27. }
  28. return macAddress;
  29. }
  30. }
  31. }