SystemInfo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using System.Management;
  5. using System.Runtime.InteropServices;
  6. using System.Collections.Generic;
  7. namespace BaseLibRWFile
  8. {
  9. ///
  10. /// 系统信息类 - 获取CPU、内存、磁盘、进程信息
  11. ///
  12. public class SystemInfo
  13. {
  14. private int m_ProcessorCount = 0; //CPU个数
  15. private PerformanceCounter pcCpuLoad; //CPU计数器
  16. private long 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. private const int KB_DIV = 1024;
  23. private const int MB_DIV = 1024 * 1024;
  24. private const int GB_DIV = 1024 * 1024 * 1024;
  25. //上次记录CPU的时间
  26. TimeSpan prevCpuTime = TimeSpan.Zero;
  27. #region AIP声明
  28. [DllImport("IpHlpApi.dll")]
  29. extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder);
  30. [DllImport("User32")]
  31. private extern static int GetWindow(int hWnd, int wCmd);
  32. [DllImport("User32")]
  33. private extern static int GetWindowLongA(int hWnd, int wIndx);
  34. [DllImport("user32.dll")]
  35. private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
  36. [DllImport("user32", CharSet = CharSet.Auto)]
  37. private extern static int GetWindowTextLength(IntPtr hWnd);
  38. #endregion
  39. #region 构造函数
  40. ///
  41. /// 构造函数,初始化计数器等
  42. ///
  43. public SystemInfo()
  44. {
  45. //初始化CPU计数器
  46. pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  47. pcCpuLoad.MachineName = ".";
  48. pcCpuLoad.NextValue();
  49. //CPU个数
  50. m_ProcessorCount = Environment.ProcessorCount;
  51. //获得物理内存
  52. ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
  53. ManagementObjectCollection moc = mc.GetInstances();
  54. foreach (ManagementObject mo in moc)
  55. {
  56. if (mo["TotalPhysicalMemory"] != null)
  57. {
  58. m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
  59. }
  60. }
  61. }
  62. #endregion
  63. #region CPU个数
  64. ///
  65. /// 获取CPU个数
  66. ///
  67. public int ProcessorCount
  68. {
  69. get
  70. {
  71. return m_ProcessorCount;
  72. }
  73. }
  74. #endregion
  75. #region CPU占用率
  76. ///
  77. /// 获取CPU占用率
  78. ///
  79. public float CpuLoad//系统CPU使用率
  80. {
  81. get
  82. {
  83. return pcCpuLoad.NextValue();
  84. }
  85. }
  86. #endregion
  87. #region 可用内存
  88. ///
  89. /// 获取可用内存
  90. ///
  91. public long MemoryAvailable
  92. {
  93. get
  94. {
  95. long availablebytes = 0;
  96. //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory");
  97. //foreach (ManagementObject mo in mos.Get())
  98. //{
  99. // availablebytes = long.Parse(mo["Availablebytes"].ToString());
  100. //}
  101. ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
  102. foreach (ManagementObject mo in mos.GetInstances())
  103. {
  104. if (mo["FreePhysicalMemory"] != null)
  105. {
  106. availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
  107. }
  108. }
  109. return availablebytes;
  110. }
  111. }
  112. #endregion
  113. #region 物理内存
  114. ///
  115. /// 获取物理内存
  116. ///
  117. public long PhysicalMemory
  118. {
  119. get
  120. {
  121. return m_PhysicalMemory;
  122. }
  123. }
  124. #endregion
  125. #region 结束指定进程
  126. /// <summary>
  127. /// 结束指定进程
  128. /// </summary>
  129. /// <param name="pid">进程的 Process ID</param>
  130. public static void EndProcess(int pid)
  131. {
  132. try
  133. {
  134. Process process = Process.GetProcessById(pid);
  135. process.Kill();
  136. }
  137. catch { }
  138. }
  139. #endregion
  140. #region 查找所有应用程序标题
  141. /// <summary>
  142. /// 查找所有应用程序标题
  143. /// </summary>
  144. /// <returns>应用程序标题范型</returns>
  145. public static List<string> FindAllApps(int Handle)
  146. {
  147. List<string> Apps = new List<string>();
  148. int hwCurr;
  149. hwCurr = GetWindow(Handle, GW_HWNDFIRST);
  150. while (hwCurr > 0)
  151. {
  152. int IsTask = (WS_VISIBLE | WS_BORDER);
  153. int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
  154. bool TaskWindow = ((lngStyle & IsTask) == IsTask);
  155. if (TaskWindow)
  156. {
  157. int length = GetWindowTextLength(new IntPtr(hwCurr));
  158. StringBuilder sb = new StringBuilder(2 * length + 1);
  159. GetWindowText(hwCurr, sb, sb.Capacity);
  160. string strTitle = sb.ToString();
  161. if (!string.IsNullOrEmpty(strTitle))
  162. {
  163. Apps.Add(strTitle);
  164. }
  165. }
  166. hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
  167. }
  168. return Apps;
  169. }
  170. #endregion
  171. #region new
  172. private Process processCurrent;
  173. private PerformanceCounter performanceCurrent;
  174. public void GetCurrentMemoryIni()
  175. {
  176. processCurrent = Process.GetCurrentProcess();
  177. performanceCurrent = new PerformanceCounter("Process", "Working Set", processCurrent.ProcessName);
  178. }
  179. public string GetCurrentMemory()
  180. {
  181. //当前时间
  182. TimeSpan curCpuTime = processCurrent.TotalProcessorTime;
  183. //计算
  184. double value = (curCpuTime - prevCpuTime).TotalMilliseconds / 1000 / Environment.ProcessorCount * 100;
  185. prevCpuTime = curCpuTime;
  186. float fMemoryUsage = performanceCurrent.NextValue() / MB_DIV;
  187. return fMemoryUsage.ToString() + "," + value.ToString();
  188. }
  189. public long GetAllProcessMemory()
  190. {
  191. return (PhysicalMemory - MemoryAvailable) / MB_DIV;//系统内存使用大小 MB
  192. }
  193. #endregion
  194. /// <summary>
  195. /// 硬盘序列号
  196. /// </summary>
  197. /// <returns></returns>
  198. public static string GetHardDiskID()
  199. {
  200. try
  201. {
  202. string hdInfo = "";//硬盘序列号
  203. ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  204. hdInfo = disk.Properties["VolumeSerialNumber"].Value.ToString();
  205. disk = null;
  206. return hdInfo.Trim();
  207. }
  208. catch (Exception )
  209. {
  210. return "uHnIk";
  211. }
  212. }
  213. }
  214. }