CpuInfo.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Text.RegularExpressions;
  6. using Vinno.IUS.Common.Log;
  7. namespace Vinno.vCloud.Terminal
  8. {
  9. public enum CpuLevel
  10. {
  11. Unknown,
  12. High,
  13. Normal,
  14. Low,
  15. NormalLow,
  16. Lowest,
  17. LowestPlus
  18. }
  19. public class CpuInfo
  20. {
  21. private readonly static Dictionary<string, CpuLevel> _cpuLevels = new Dictionary<string, CpuLevel>()
  22. {
  23. { "I3-4160", CpuLevel.Normal },
  24. { "I3-4170", CpuLevel.Normal},
  25. { "I5-4570", CpuLevel.High},
  26. { "I5-4590", CpuLevel.High},
  27. { "I3-6100", CpuLevel.Normal},
  28. { "I5-6500", CpuLevel.High},
  29. { "G5400", CpuLevel.Normal},
  30. { "I3-8100", CpuLevel.Normal},
  31. { "I5-8400", CpuLevel.High},
  32. { "I7-4650U", CpuLevel.Low},
  33. { "I3-6100U", CpuLevel.Low},
  34. { "I5-6300U", CpuLevel.NormalLow},
  35. { "I3-7100U", CpuLevel.NormalLow},
  36. { "PENTIUM(R)", CpuLevel.LowestPlus},
  37. { "I7-9700", CpuLevel.High}
  38. };
  39. private static CpuLevel _currentCpuLevel = CpuLevel.Unknown;
  40. private static string _processorId;
  41. private static string _machineId;
  42. public static CpuLevel CurrentCpuLevel
  43. {
  44. get
  45. {
  46. return _currentCpuLevel;
  47. }
  48. }
  49. /// <summary>
  50. /// Initialize to cpuinfo ,must to initialize for app
  51. /// </summary>
  52. public static void Initialize()
  53. {
  54. _currentCpuLevel =GetCpuLevel();
  55. _machineId = GetSystemDiskNo();
  56. }
  57. /// <summary>
  58. /// Processor ID
  59. /// </summary>
  60. public static string Id
  61. {
  62. get
  63. {
  64. if (string.IsNullOrEmpty(_processorId))
  65. {
  66. _processorId = GetCpuID();
  67. }
  68. return _processorId;
  69. }
  70. }
  71. /// <summary>
  72. /// Machine ID
  73. /// </summary>
  74. public static string MachineId
  75. {
  76. get
  77. {
  78. if (string.IsNullOrEmpty(_machineId))
  79. {
  80. _machineId = GetSystemDiskNo();
  81. }
  82. return _machineId;
  83. }
  84. }
  85. private static CpuLevel GetCpuLevel()
  86. {
  87. var cpuLevel = CpuLevel.Lowest;
  88. try
  89. {
  90. var cpuName = GetCpuName();//spend 200ms-1s.
  91. Logger.WriteLineInfo($"CpuName:{cpuName}");
  92. if (!string.IsNullOrEmpty(cpuName))
  93. {
  94. if (_cpuLevels.ContainsKey(cpuName))
  95. {
  96. cpuLevel = _cpuLevels[cpuName];
  97. }
  98. else
  99. {
  100. var cpus = cpuName.Split('-');
  101. if (cpus.Length > 1)
  102. {
  103. var cpuCoreLevel = Convert.ToInt32(Regex.Replace(cpus[0], @"[^\d.\d]", ""));
  104. if (cpuCoreLevel > 5)
  105. {
  106. cpuLevel = CpuLevel.High;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. Logger.WriteLineError($"Terminal GetCpuLevel failed ,to set { CpuLevel.Lowest} ex:{ex}");
  115. }
  116. return cpuLevel;
  117. }
  118. private static string GetCpuName()
  119. {
  120. ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
  121. var cpus = searcher.Get();
  122. foreach (var cpu in cpus)
  123. {
  124. var cpuName = cpu["Name"].ToString();
  125. //Try get cpu level.
  126. var keywords = cpuName.Split(' ');
  127. var keywordList = new List<string>();
  128. foreach (var keyword in keywords)
  129. {
  130. keywordList.Add(keyword.Trim());
  131. }
  132. var cpuKeyWordIndex = keywordList.IndexOf("CPU");
  133. if (cpuKeyWordIndex != -1)
  134. {
  135. if (cpuKeyWordIndex - 1 >= 0)
  136. {
  137. var cpuModelKeyword = keywordList[cpuKeyWordIndex - 1];
  138. return cpuModelKeyword.ToUpper();
  139. }
  140. }
  141. }
  142. return null;
  143. }
  144. static string GetCpuID()
  145. {
  146. try
  147. {
  148. string processorId = "";//cpu ID
  149. ManagementClass mc = new ManagementClass("Win32_Processor");
  150. ManagementObjectCollection moc = mc.GetInstances();
  151. foreach (ManagementObject mo in moc)
  152. {
  153. processorId = mo.Properties["ProcessorId"].Value.ToString();
  154. if (!string.IsNullOrEmpty(processorId))
  155. {
  156. break;
  157. }
  158. }
  159. moc.Dispose();
  160. moc = null;
  161. mc.Dispose();
  162. mc = null;
  163. return processorId;
  164. }
  165. catch(Exception e)
  166. {
  167. Logger.WriteLineError($"Get ProcessorId ex:{e}");
  168. }
  169. return string.Empty;
  170. }
  171. /// <summary>
  172. /// 获取系统硬盘ID
  173. /// </summary>
  174. /// <returns></returns>
  175. public static string GetSystemDiskNo()
  176. {
  177. try
  178. {
  179. ManagementClass cimObject = new ManagementClass("Win32_PhysicalMedia");
  180. ManagementObjectCollection moc = cimObject.GetInstances();
  181. Dictionary<string, string> dict = new Dictionary<string, string>();
  182. foreach (ManagementObject mo in moc)
  183. {
  184. string tag = mo.Properties["Tag"].Value.ToString().ToLower().Trim();
  185. string hdId = (string)mo.Properties["SerialNumber"].Value ?? string.Empty;
  186. hdId = hdId.Trim();
  187. dict.Add(tag, hdId);
  188. }
  189. cimObject = new ManagementClass("Win32_OperatingSystem");
  190. moc = cimObject.GetInstances();
  191. string currentSysRunDisk = string.Empty;
  192. foreach (ManagementObject mo in moc)
  193. {
  194. currentSysRunDisk = Regex.Match(mo.Properties["Name"].Value.ToString().ToLower(), @"harddisk\d+").Value;
  195. }
  196. var results = dict.Where(x => Regex.IsMatch(x.Key, @"physicaldrive" + Regex.Match(currentSysRunDisk, @"\d+$").Value));
  197. moc.Dispose();
  198. moc = null;
  199. if (results.Any())
  200. {
  201. var id = results.ElementAt(0).Value;
  202. id = Regex.Replace(id, "[^a-zA-Z0-9]", "");
  203. return id;
  204. };
  205. }
  206. catch (Exception ex)
  207. {
  208. }
  209. return "";
  210. }
  211. /// <summary>
  212. /// 获取主板ID
  213. /// </summary>
  214. /// <returns></returns>
  215. public static string GetMainBordID()
  216. {
  217. ManagementClass mc = new ManagementClass("Win32_BaseBoard");
  218. ManagementObjectCollection moc = mc.GetInstances();
  219. string strID = null;
  220. foreach (ManagementObject mo in moc)
  221. {
  222. strID = mo.Properties["SerialNumber"].Value.ToString();
  223. break;
  224. }
  225. return strID;
  226. }
  227. }
  228. }