CNAS取数仪器端升级
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

132 wiersze
4.5KB

  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Management;
  9. using System.Net.NetworkInformation;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. namespace CnasSynchronousCommon
  13. {
  14. public class ComputeMessage
  15. {
  16. /// <summary>
  17. /// 获取本地MAC地址
  18. /// </summary>
  19. /// <returns></returns>
  20. public static string getLocalMac(ref string strErrorMsg)
  21. {
  22. string madAddr = null;
  23. List<string> lstMacs = new List<string>();
  24. try
  25. {
  26. string strMethod=ConfigurationManager.AppSettings["GetMacMethod"].ToString();
  27. switch (strMethod)
  28. {
  29. case "2":
  30. lstMacs=GetMacByIPConfig();
  31. if (lstMacs.Count > 0)
  32. madAddr = lstMacs[0];
  33. break;
  34. case "3":
  35. lstMacs = GetMacByNetworkInterface();
  36. if (lstMacs.Count > 0)
  37. madAddr = lstMacs[0];
  38. break;
  39. case "1":
  40. default:
  41. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  42. ManagementObjectCollection moc2 = mc.GetInstances();
  43. foreach (ManagementObject mo in moc2)
  44. {
  45. if (Convert.ToBoolean(mo["IPEnabled"]) == true)
  46. {
  47. madAddr = mo["MacAddress"].ToString();
  48. madAddr = madAddr.Replace(':', '-');
  49. }
  50. mo.Dispose();
  51. }
  52. break;
  53. }
  54. if (madAddr == null)
  55. {
  56. strErrorMsg = "未能成功获得本机网卡的物理地址";
  57. AppLog.Error($"没有找到本地网卡的物理地址");
  58. return "unknown";
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. //这里写异常的处理
  64. strErrorMsg = "未能成功获得本机网卡的物理地址";
  65. AppLog.Error($"没有找到本地网卡的物理地址,{ex.Message}");
  66. }
  67. return madAddr;
  68. }
  69. ///<summary>
  70. /// 根据截取ipconfig /all命令的输出流获取网卡Mac
  71. ///</summary>
  72. ///<returns></returns>
  73. public static List<string> GetMacByIPConfig()
  74. {
  75. List<string> macs = new List<string>();
  76. ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig", "/all");
  77. startInfo.UseShellExecute = false;
  78. startInfo.RedirectStandardInput = true;
  79. startInfo.RedirectStandardOutput = true;
  80. startInfo.RedirectStandardError = true;
  81. startInfo.CreateNoWindow = true;
  82. Process p = Process.Start(startInfo);
  83. //截取输出流
  84. StreamReader reader = p.StandardOutput;
  85. string line = reader.ReadLine();
  86. while (!reader.EndOfStream)
  87. {
  88. if (!string.IsNullOrEmpty(line))
  89. {
  90. line = line.Trim();
  91. if (line.StartsWith("Physical Address")|| line.StartsWith("物理地址"))
  92. {
  93. if(!macs.Contains(line))
  94. macs.Add(line);
  95. }
  96. }
  97. line = reader.ReadLine();
  98. }
  99. //等待程序执行完退出进程
  100. p.WaitForExit();
  101. p.Close();
  102. reader.Close();
  103. return macs;
  104. }
  105. ///<summary>
  106. /// 通过NetworkInterface读取网卡Mac
  107. ///</summary>
  108. ///<returns></returns>
  109. public static List<string> GetMacByNetworkInterface()
  110. {
  111. List<string> macs = new List<string>();
  112. NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
  113. foreach (NetworkInterface ni in interfaces)
  114. {
  115. if(!macs.Contains(ni.GetPhysicalAddress().ToString()))
  116. macs.Add(ni.GetPhysicalAddress().ToString());
  117. }
  118. return macs;
  119. }
  120. }
  121. }