Computer.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 WebAPIBase.Utils
  8. {
  9. public class Computer
  10. {
  11. /// <summary>
  12. /// cpu序列号
  13. /// </summary>
  14. public static string CpuID; //1.cpu序列号
  15. /// <summary>
  16. /// mac序列号
  17. /// </summary>
  18. public static string MacAddress; //2.mac序列号
  19. /// <summary>
  20. /// 硬盘id
  21. /// </summary>
  22. public static string DiskID; //3.硬盘id
  23. /// <summary>
  24. /// .ip地址
  25. /// </summary>
  26. public static string IpAddress; //4.ip地址
  27. /// <summary>
  28. /// 内存量 单位:M
  29. /// </summary>
  30. public static string TotalPhysicalMemory; //8.内存量 单位:M
  31. /// <summary>
  32. /// 主板
  33. /// </summary>
  34. public static string MotherboardID;
  35. static Computer()
  36. {
  37. CpuID = GetCpuID();
  38. MacAddress = GetMacAddress();
  39. DiskID = GetDiskID();
  40. IpAddress = GetIPAddress();
  41. MotherboardID = GetMotherboardID();
  42. TotalPhysicalMemory = GetTotalPhysicalMemory();
  43. }
  44. /// <summary>
  45. /// 获取CPU序列号代码
  46. /// </summary>
  47. /// <returns></returns>
  48. static string GetCpuID()
  49. {
  50. try
  51. {
  52. string cpuInfo = "";//cpu序列号
  53. ManagementClass mc = new ManagementClass("Win32_Processor");
  54. ManagementObjectCollection moc = mc.GetInstances();
  55. foreach (ManagementObject mo in moc)
  56. {
  57. cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
  58. }
  59. moc = null;
  60. mc = null;
  61. return cpuInfo;
  62. }
  63. catch
  64. {
  65. return "unknow";
  66. }
  67. finally
  68. {
  69. }
  70. }
  71. /// <summary>
  72. /// 获取网卡硬件地址
  73. /// </summary>
  74. /// <returns></returns>
  75. static string GetMacAddress()
  76. {
  77. try
  78. {
  79. string mac = "";
  80. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  81. ManagementObjectCollection moc = mc.GetInstances();
  82. foreach (ManagementObject mo in moc)
  83. {
  84. if ((bool)mo["IPEnabled"] == true)
  85. {
  86. mac = mo["MacAddress"].ToString();
  87. break;
  88. }
  89. }
  90. moc = null;
  91. mc = null;
  92. return mac;
  93. }
  94. catch
  95. {
  96. return "unknow";
  97. }
  98. finally
  99. {
  100. }
  101. }
  102. /// <summary>
  103. /// 获取硬盘ID
  104. /// </summary>
  105. /// <returns></returns>
  106. static string GetDiskID()
  107. {
  108. try
  109. {
  110. String HDid = "";
  111. ManagementClass mc = new ManagementClass("Win32_DiskDrive");
  112. ManagementObjectCollection moc = mc.GetInstances();
  113. foreach (ManagementObject mo in moc)
  114. {
  115. HDid = (string)mo.Properties["Model"].Value;
  116. }
  117. moc = null;
  118. mc = null;
  119. return HDid;
  120. }
  121. catch
  122. {
  123. return "unknow";
  124. }
  125. finally
  126. {
  127. }
  128. }
  129. /// <summary>
  130. /// 获取主板序列号
  131. /// </summary>
  132. /// <returns></returns>
  133. private static string GetMotherboardID()
  134. {
  135. var myMb = new ManagementClass("Win32_BaseBoard").GetInstances();
  136. var serial = "";
  137. foreach (ManagementObject mb in myMb)
  138. {
  139. var val = mb.Properties["SerialNumber"].Value;
  140. serial += val == null ? "" : val.ToString();
  141. }
  142. return serial;
  143. }
  144. /// <summary>
  145. /// 获取IP地址
  146. /// </summary>
  147. /// <returns></returns>
  148. static string GetIPAddress()
  149. {
  150. try
  151. {
  152. string st = "";
  153. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  154. ManagementObjectCollection moc = mc.GetInstances();
  155. foreach (ManagementObject mo in moc)
  156. {
  157. if ((bool)mo["IPEnabled"] == true)
  158. {
  159. //st=mo["IpAddress"].ToString();
  160. System.Array ar;
  161. ar = (System.Array)(mo.Properties["IpAddress"].Value);
  162. st = ar.GetValue(0).ToString();
  163. break;
  164. }
  165. }
  166. moc = null;
  167. mc = null;
  168. return st;
  169. }
  170. catch
  171. {
  172. return "unknow";
  173. }
  174. finally
  175. {
  176. }
  177. }
  178. /// <summary>
  179. /// 操作系统的登录用户名
  180. /// </summary>
  181. /// <returns></returns>
  182. static string GetUserName()
  183. {
  184. try
  185. {
  186. string un = "";
  187. un = Environment.UserName;
  188. return un;
  189. }
  190. catch
  191. {
  192. return "unknow";
  193. }
  194. finally
  195. {
  196. }
  197. }
  198. /// <summary>
  199. /// 获取计算机名
  200. /// </summary>
  201. /// <returns></returns>
  202. static string GetComputerName()
  203. {
  204. try
  205. {
  206. return System.Environment.MachineName;
  207. }
  208. catch
  209. {
  210. return "unknow";
  211. }
  212. finally
  213. {
  214. }
  215. }
  216. /// <summary>
  217. /// PC类型
  218. /// </summary>
  219. /// <returns></returns>
  220. static string GetSystemType()
  221. {
  222. try
  223. {
  224. string st = "";
  225. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  226. ManagementObjectCollection moc = mc.GetInstances();
  227. foreach (ManagementObject mo in moc)
  228. {
  229. st = mo["SystemType"].ToString();
  230. }
  231. moc = null;
  232. mc = null;
  233. return st;
  234. }
  235. catch
  236. {
  237. return "unknow";
  238. }
  239. finally
  240. {
  241. }
  242. }
  243. /// <summary>
  244. /// 物理内存
  245. /// </summary>
  246. /// <returns></returns>
  247. static string GetTotalPhysicalMemory()
  248. {
  249. try
  250. {
  251. string st = "";
  252. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  253. ManagementObjectCollection moc = mc.GetInstances();
  254. foreach (ManagementObject mo in moc)
  255. {
  256. st = mo["TotalPhysicalMemory"].ToString();
  257. }
  258. moc = null;
  259. mc = null;
  260. return st;
  261. }
  262. catch
  263. {
  264. return "unknow";
  265. }
  266. finally
  267. {
  268. }
  269. }
  270. /// <summary>
  271. /// 根据cpu序列化,主板id及内存信息生成16为授权码(16位HASH代码)
  272. /// </summary>
  273. /// <returns></returns>
  274. public static string AuthorizationCode(string cpu, string motherBoard, string physicalMemory)
  275. {
  276. string sensorID = string.Empty;
  277. string basicID = cpu + motherBoard + physicalMemory;
  278. System.Security.Cryptography.MD5CryptoServiceProvider mD5CryptoServiceProvider = new System.Security.Cryptography.MD5CryptoServiceProvider();
  279. byte[] hashedBuff = mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(basicID));
  280. for (int i = 4; i < 12; i++)
  281. {
  282. sensorID += hashedBuff[i].ToString("X2");
  283. }
  284. return sensorID;
  285. }
  286. }
  287. }