IPHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MainForm.ClassFile.ProjectClass
  8. {
  9. public class IPHelper
  10. {
  11. ///<summary>
  12. ///获取网卡硬件地址
  13. ///</summary>
  14. ///<returns> string </returns>
  15. public static string GetMoAddress()
  16. {
  17. string MoAddress = "";
  18. try
  19. {
  20. using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
  21. {
  22. ManagementObjectCollection moc2 = mc.GetInstances();
  23. foreach (ManagementObject mo in moc2)
  24. {
  25. if ((bool)mo["IPEnabled"] == true)
  26. MoAddress = mo["MacAddress"].ToString();
  27. mo.Dispose();
  28. }
  29. }
  30. }
  31. catch (Exception)
  32. {
  33. throw;
  34. }
  35. return MoAddress.ToString();
  36. }
  37. }
  38. }