SystemInfo.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Management;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace UAS_MES_NEW.PublicMethod
  8. {
  9. class SystemInfo
  10. {
  11. private int m_ProcessorCount = 0; //CPU个数
  12. private PerformanceCounter pcCpuLoad; //CPU计数器
  13. private long m_PhysicalMemory = 0; //物理内存
  14. private const int GW_HWNDFIRST = 0;
  15. private const int GW_HWNDNEXT = 2;
  16. private const int GWL_STYLE = (-16);
  17. private const int WS_VISIBLE = 268435456;
  18. private const int WS_BORDER = 8388608;
  19. #region AIP声明
  20. [DllImport("IpHlpApi.dll")]
  21. extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
  22. [DllImport("User32")]
  23. private extern static int GetWindow(int hWnd, int wCmd);
  24. [DllImport("User32")]
  25. private extern static int GetWindowLongA(int hWnd, int wIndx);
  26. [DllImport("user32.dll")]
  27. private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
  28. [DllImport("user32", CharSet = CharSet.Auto)]
  29. private extern static int GetWindowTextLength(IntPtr hWnd);
  30. #endregion
  31. #region 构造函数
  32. /// <summary>
  33. /// 构造函数,初始化计数器等
  34. /// </summary>
  35. public SystemInfo()
  36. {
  37. //初始化CPU计数器
  38. pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  39. pcCpuLoad.MachineName = ".";
  40. pcCpuLoad.NextValue();
  41. //CPU个数
  42. m_ProcessorCount = Environment.ProcessorCount;
  43. //获得物理内存
  44. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  45. ManagementObjectCollection moc = mc.GetInstances();
  46. foreach (ManagementObject mo in moc)
  47. {
  48. if (mo["TotalPhysicalMemory"] != null)
  49. {
  50. m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
  51. }
  52. }
  53. }
  54. #endregion
  55. #region CPU个数
  56. /// <summary>
  57. /// 获取CPU个数
  58. /// </summary>
  59. public int ProcessorCount
  60. {
  61. get
  62. {
  63. return m_ProcessorCount;
  64. }
  65. }
  66. #endregion
  67. #region CPU占用率
  68. /// <summary>
  69. /// 获取CPU占用率
  70. /// </summary>
  71. public float CpuLoad
  72. {
  73. get
  74. {
  75. return pcCpuLoad.NextValue();
  76. }
  77. }
  78. #endregion
  79. #region 可用内存
  80. /// <summary>
  81. /// 获取可用内存
  82. /// </summary>
  83. public long MemoryAvailable
  84. {
  85. get
  86. {
  87. long availablebytes = 0;
  88. //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory");
  89. //foreach (ManagementObject mo in mos.Get())
  90. //{
  91. // availablebytes = long.Parse(mo["Availablebytes"].ToString());
  92. //}
  93. ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
  94. foreach (ManagementObject mo in mos.GetInstances())
  95. {
  96. if (mo["FreePhysicalMemory"] != null)
  97. {
  98. availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
  99. }
  100. }
  101. return availablebytes;
  102. }
  103. }
  104. #endregion
  105. #region 物理内存
  106. /// <summary>
  107. /// 获取物理内存
  108. /// </summary>
  109. public long PhysicalMemory
  110. {
  111. get
  112. {
  113. return m_PhysicalMemory;
  114. }
  115. }
  116. #endregion
  117. #region 结束指定进程
  118. /// <summary>
  119. /// 结束指定进程
  120. /// </summary>
  121. /// <param name="pid">进程的 Process ID</param>
  122. public static void EndProcess(int pid)
  123. {
  124. try
  125. {
  126. Process process = Process.GetProcessById(pid);
  127. process.Kill();
  128. }
  129. catch { }
  130. }
  131. #endregion
  132. #region 查找所有应用程序标题
  133. /// <summary>
  134. /// 查找所有应用程序标题
  135. /// </summary>
  136. /// <returns>应用程序标题范型</returns>
  137. public static List<string> FindAllApps(int Handle)
  138. {
  139. List<string> Apps = new List<string>();
  140. int hwCurr;
  141. hwCurr = GetWindow(Handle, GW_HWNDFIRST);
  142. while (hwCurr > 0)
  143. {
  144. int IsTask = (WS_VISIBLE | WS_BORDER);
  145. int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
  146. bool TaskWindow = ((lngStyle & IsTask) == IsTask);
  147. if (TaskWindow)
  148. {
  149. int length = GetWindowTextLength(new IntPtr(hwCurr));
  150. StringBuilder sb = new StringBuilder(2 * length + 1);
  151. GetWindowText(hwCurr, sb, sb.Capacity);
  152. string strTitle = sb.ToString();
  153. if (!string.IsNullOrEmpty(strTitle))
  154. {
  155. Apps.Add(strTitle);
  156. }
  157. }
  158. hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
  159. }
  160. return Apps;
  161. }
  162. #endregion
  163. }
  164. }