MacAccess.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ET
  9. {
  10. public static class MacAccess
  11. {
  12. /// <summary>
  13. /// 根据截取ipconfig /all命令的输出流获取网卡Mac
  14. /// </summary>
  15. /// <returns></returns>
  16. public static List<string> GetMacByIPConfig()
  17. {
  18. List<string> macs = new List<string>();
  19. ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig", "/all");
  20. startInfo.UseShellExecute = false;
  21. startInfo.RedirectStandardInput = true;
  22. startInfo.RedirectStandardOutput = true;
  23. startInfo.RedirectStandardError = true;
  24. startInfo.CreateNoWindow = true;
  25. Process p = Process.Start(startInfo);
  26. // 截取输出流
  27. StreamReader reader = p.StandardOutput;
  28. string line = reader.ReadLine();
  29. while (!reader.EndOfStream)
  30. {
  31. if (!string.IsNullOrEmpty(line))
  32. {
  33. line = line.Trim();
  34. if (line.StartsWith(" Physical Address "))
  35. {
  36. macs.Add(line);
  37. }
  38. }
  39. line = reader.ReadLine();
  40. }
  41. // 等待程序执行完退出进程
  42. p.WaitForExit();
  43. p.Close();
  44. reader.Close();
  45. return macs;
  46. }
  47. }
  48. }