濮阳12345API

SysInformationHelper.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Runtime.InteropServices;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CallCenter.Utility
  11. {
  12. public class SysInformationHelper
  13. {
  14. private int m_ProcessorCount = 0; //CPU个数
  15. private PerformanceCounter pcCpuLoad; //CPU计数器
  16. private float m_PhysicalMemory = 0; //物理内存
  17. private const int GW_HWNDFIRST = 0;
  18. private const int GW_HWNDNEXT = 2;
  19. private const int GWL_STYLE = (-16);
  20. private const int WS_VISIBLE = 268435456;
  21. private const int WS_BORDER = 8388608;
  22. #region AIP声明
  23. [DllImport("IpHlpApi.dll")]
  24. extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
  25. [DllImport("User32")]
  26. private extern static int GetWindow(int hWnd, int wCmd);
  27. [DllImport("User32")]
  28. private extern static int GetWindowLongA(int hWnd, int wIndx);
  29. [DllImport("user32.dll")]
  30. private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
  31. [DllImport("user32", CharSet = CharSet.Auto)]
  32. private extern static int GetWindowTextLength(IntPtr hWnd);
  33. #endregion
  34. // 构造函数,初始化计数器等
  35. public SysInformationHelper()
  36. {
  37. //初始化CPU计数器
  38. pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  39. pcCpuLoad.MachineName = ".";
  40. pcCpuLoad.NextValue();
  41. System.Threading.Thread.Sleep(1000); // wait for 1 second
  42. //CPU个数
  43. m_ProcessorCount = Environment.ProcessorCount;
  44. //获得物理内存
  45. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  46. ManagementObjectCollection moc = mc.GetInstances();
  47. foreach (ManagementObject mo in moc)
  48. {
  49. if (mo["TotalPhysicalMemory"] != null)
  50. {
  51. m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
  52. }
  53. }
  54. }
  55. #region CPU个数
  56. // 获取CPU个数
  57. public int ProcessorCount
  58. {
  59. get
  60. {
  61. return m_ProcessorCount;
  62. }
  63. }
  64. #endregion
  65. #region CPU占用率
  66. // 获取CPU占用率
  67. public float CpuLoad
  68. {
  69. get
  70. {
  71. return pcCpuLoad.NextValue();
  72. }
  73. }
  74. #endregion
  75. #region 可用内存/物理内存
  76. // 获取可用内存(RAM)
  77. public float MemoryAvailable
  78. {
  79. get
  80. {
  81. float availablebytes = 0;
  82. ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
  83. foreach (ManagementObject mo in mos.GetInstances())
  84. {
  85. if (mo["FreePhysicalMemory"] != null)
  86. {
  87. availablebytes = 1024 * float.Parse(mo["FreePhysicalMemory"].ToString());
  88. }
  89. }
  90. return availablebytes;
  91. }
  92. }
  93. // 获取物理内存(RAM)
  94. public float PhysicalMemory
  95. {
  96. get
  97. {
  98. return m_PhysicalMemory;
  99. }
  100. }
  101. #endregion
  102. /// <summary>
  103. /// 硬盘使用百分比
  104. /// </summary>
  105. /// <param name="HardDisc"></param>
  106. /// <returns></returns>
  107. public float GetDrivePer1(string HardDisc)
  108. {
  109. System.IO.DriveInfo[] drives = System.IO.Directory.GetLogicalDrives().Select(q => new System.IO.DriveInfo(q)).ToArray();
  110. float ret = 0;
  111. double total = 0, ones = 0;
  112. HardDisc = HardDisc + ":\\";
  113. foreach (var driveInfo in drives)
  114. {
  115. if (driveInfo.IsReady && driveInfo.Name == HardDisc)
  116. {
  117. ones = ConverDiskSpace(driveInfo.TotalSize) - ConverDiskSpace(driveInfo.TotalFreeSpace);
  118. total = ConverDiskSpace(driveInfo.TotalSize);
  119. }
  120. }
  121. if (ones > 0 && total > 0)
  122. {
  123. ret = (float)((ones * 100) / total);
  124. }
  125. return ret;
  126. }
  127. /// <summary>
  128. /// 磁盘大小换算
  129. /// </summary>
  130. /// <param name="bytesNumber"></param>
  131. /// <returns></returns>
  132. public static double ConverDiskSpace(long bytesNumber)
  133. {
  134. long value = 0;
  135. double ret = 0;
  136. if (((value = (long)(((bytesNumber / (1024 * 1024 * 1024)) * (1.0 / 1024)))) >= 1))
  137. {
  138. ret = (double)(bytesNumber / (1024 * 1024 * 1024)) * (1.0 / 1024);
  139. }
  140. else if ((value = bytesNumber / (1024 * 1024 * 1024)) >= 1)
  141. {
  142. ret = (double)bytesNumber / (1024 * 1024 * 1024);
  143. }
  144. else if ((value = bytesNumber / (1024 * 1024)) >= 1)
  145. {
  146. ret = (double)bytesNumber / (1024 * 1024);
  147. }
  148. else if ((value = bytesNumber / (1024)) >= 1)
  149. {
  150. ret = (double)bytesNumber / (1024);
  151. }
  152. else
  153. {
  154. ret = (double)bytesNumber;
  155. }
  156. return ret;
  157. }
  158. #region 计算机的唯一标识代码
  159. /// <summary>
  160. /// 16字节的计算机的唯一标识代码
  161. /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
  162. /// </summary>
  163. /// <returns></returns>
  164. public static string GetPCUniqueIdentificationCode()
  165. {
  166. string fingerPrint = string.Empty;
  167. if (string.IsNullOrEmpty(fingerPrint))
  168. {
  169. fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " +
  170. biosId() + "\nBASE >> " + baseId() +
  171. videoId() + "\nMAC >> " + macId()
  172. );
  173. //+"\nDISK >> "+ diskId() + "\nVIDEO >> " +
  174. }
  175. return fingerPrint;
  176. }
  177. private static string GetHash(string s)
  178. {
  179. MD5 sec = new MD5CryptoServiceProvider();
  180. ASCIIEncoding enc = new ASCIIEncoding();
  181. byte[] bt = enc.GetBytes(s);
  182. return GetHexString(sec.ComputeHash(bt));
  183. }
  184. private static string GetHexString(byte[] bt)
  185. {
  186. string s = string.Empty;
  187. for (int i = 0; i < bt.Length; i++)
  188. {
  189. byte b = bt[i];
  190. int n, n1, n2;
  191. n = (int)b;
  192. n1 = n & 15;
  193. n2 = (n >> 4) & 15;
  194. if (n2 > 9)
  195. s += ((char)(n2 - 10 + (int)'A')).ToString();
  196. else
  197. s += n2.ToString();
  198. if (n1 > 9)
  199. s += ((char)(n1 - 10 + (int)'A')).ToString();
  200. else
  201. s += n1.ToString();
  202. if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
  203. }
  204. return s;
  205. }
  206. #region Original Device ID Getting Code
  207. //Return a hardware identifier
  208. private static string identifier
  209. (string wmiClass, string wmiProperty, string wmiMustBeTrue)
  210. {
  211. string result = "";
  212. System.Management.ManagementClass mc =
  213. new System.Management.ManagementClass(wmiClass);
  214. System.Management.ManagementObjectCollection moc = mc.GetInstances();
  215. foreach (System.Management.ManagementObject mo in moc)
  216. {
  217. if (mo[wmiMustBeTrue].ToString() == "True")
  218. {
  219. //Only get the first one
  220. if (result == "")
  221. {
  222. try
  223. {
  224. result = mo[wmiProperty].ToString();
  225. break;
  226. }
  227. catch
  228. {
  229. }
  230. }
  231. }
  232. }
  233. return result;
  234. }
  235. //Return a hardware identifier
  236. private static string identifier(string wmiClass, string wmiProperty)
  237. {
  238. string result = "";
  239. System.Management.ManagementClass mc =
  240. new System.Management.ManagementClass(wmiClass);
  241. System.Management.ManagementObjectCollection moc = mc.GetInstances();
  242. foreach (System.Management.ManagementObject mo in moc)
  243. {
  244. //Only get the first one
  245. if (result == "")
  246. {
  247. try
  248. {
  249. result = mo[wmiProperty].ToString();
  250. break;
  251. }
  252. catch
  253. {
  254. }
  255. }
  256. }
  257. return result;
  258. }
  259. private static string cpuId()
  260. {
  261. //Uses first CPU identifier available in order of preference
  262. //Don't get all identifiers, as it is very time consuming
  263. string retVal = identifier("Win32_Processor", "UniqueId");
  264. if (retVal == "") //If no UniqueID, use ProcessorID
  265. {
  266. retVal = identifier("Win32_Processor", "ProcessorId");
  267. if (retVal == "") //If no ProcessorId, use Name
  268. {
  269. retVal = identifier("Win32_Processor", "Name");
  270. if (retVal == "") //If no Name, use Manufacturer
  271. {
  272. retVal = identifier("Win32_Processor", "Manufacturer");
  273. }
  274. //Add clock speed for extra security
  275. retVal += identifier("Win32_Processor", "MaxClockSpeed");
  276. }
  277. }
  278. return retVal;
  279. }
  280. //BIOS Identifier
  281. private static string biosId()
  282. {
  283. return identifier("Win32_BIOS", "Manufacturer")
  284. + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
  285. + identifier("Win32_BIOS", "IdentificationCode")
  286. + identifier("Win32_BIOS", "SerialNumber")
  287. + identifier("Win32_BIOS", "ReleaseDate")
  288. + identifier("Win32_BIOS", "Version");
  289. }
  290. //Main physical hard drive ID
  291. private static string diskId()
  292. {
  293. return identifier("Win32_DiskDrive", "Model")
  294. + identifier("Win32_DiskDrive", "Manufacturer")
  295. + identifier("Win32_DiskDrive", "Signature")
  296. + identifier("Win32_DiskDrive", "TotalHeads");
  297. }
  298. //Motherboard ID
  299. private static string baseId()
  300. {
  301. return identifier("Win32_BaseBoard", "Model")
  302. + identifier("Win32_BaseBoard", "Manufacturer")
  303. + identifier("Win32_BaseBoard", "Name")
  304. + identifier("Win32_BaseBoard", "SerialNumber");
  305. }
  306. //Primary video controller ID
  307. private static string videoId()
  308. {
  309. return identifier("Win32_VideoController", "DriverVersion")
  310. + identifier("Win32_VideoController", "Name");
  311. }
  312. //First enabled network card ID
  313. private static string macId()
  314. {
  315. return identifier("Win32_NetworkAdapterConfiguration",
  316. "MACAddress", "IPEnabled");
  317. }
  318. #endregion
  319. #endregion
  320. }
  321. }