1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Management; using System.Runtime.InteropServices; using System.Text; namespace ConsoleApp1 { /// /// 系统信息类 - 获取CPU、内存、磁盘、进程信息 /// public class SystemInfo { private int m_ProcessorCount = 0; //CPU个数 private PerformanceCounter pcCpuLoad; //CPU计数器 private long m_PhysicalMemory = 0; //物理内存 private const int GW_HWNDFIRST = 0; private const int GW_HWNDNEXT = 2; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 268435456; private const int WS_BORDER = 8388608; #region AIP声明 [DllImport("IpHlpApi.dll")] extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder); [DllImport("User32")] private extern static int GetWindow(int hWnd, int wCmd); [DllImport("User32")] private extern static int GetWindowLongA(int hWnd, int wIndx); [DllImport("user32.dll")] private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize); [DllImport("user32", CharSet = CharSet.Auto)] private extern static int GetWindowTextLength(IntPtr hWnd); #endregion #region 构造函数 /// /// 构造函数,初始化计数器等 /// public SystemInfo() { //初始化CPU计数器 pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total"); pcCpuLoad.MachineName = "."; pcCpuLoad.NextValue(); //CPU个数 m_ProcessorCount = Environment.ProcessorCount; //获得物理内存 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["TotalPhysicalMemory"] != null) { m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString()); } } } #endregion #region CPU个数 /// /// 获取CPU个数 /// public int ProcessorCount { get { return m_ProcessorCount; } } #endregion #region CPU占用率 /// /// 获取CPU占用率 /// public float CpuLoad { get { return pcCpuLoad.NextValue(); } } #endregion #region 可用内存 /// /// 获取可用内存 /// public long MemoryAvailable { get { long availablebytes = 0; //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory"); //foreach (ManagementObject mo in mos.Get()) //{ // availablebytes = long.Parse(mo["Availablebytes"].ToString()); //} ManagementClass mos = new ManagementClass("Win32_OperatingSystem"); foreach (ManagementObject mo in mos.GetInstances()) { if (mo["FreePhysicalMemory"] != null) { availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString()); } } return availablebytes; } } #endregion #region 物理内存 /// /// 获取物理内存 /// public long PhysicalMemory { get { return m_PhysicalMemory; } } #endregion #region 获得分区信息 /// /// 获取分区信息 /// public List<DiskInfo> GetLogicalDrives() { List<DiskInfo> drives = new List<DiskInfo>(); ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection disks = diskClass.GetInstances(); foreach (ManagementObject disk in disks) { // DriveType.Fixed 为固定磁盘(硬盘) if (int.Parse(disk["DriveType"].ToString()) == (int)DriveType.Fixed) { drives.Add(new DiskInfo(disk["Name"].ToString(), long.Parse(disk["Size"].ToString()), long.Parse(disk["FreeSpace"].ToString()))); } } return drives; } /// /// 获取特定分区信息 /// /// 盘符 public List<DiskInfo> GetLogicalDrives(char DriverID) { List<DiskInfo> drives = new List<DiskInfo>(); WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = ’" + DriverID + ":’"); ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery); foreach (ManagementObject disk in wmifind.Get()) { if (int.Parse(disk["DriveType"].ToString()) == (int)DriveType.Fixed) { drives.Add(new DiskInfo(disk["Name"].ToString(), long.Parse(disk["Size"].ToString()), long.Parse(disk["FreeSpace"].ToString()))); } } return drives; } #endregion #region 获得进程列表 /// /// 获得进程列表 /// public List<ProcessInfo> GetProcessInfo() { List<ProcessInfo> pInfo = new List<ProcessInfo>(); Process[] processes = Process.GetProcesses(); foreach (Process instance in processes) { try { pInfo.Add(new ProcessInfo(instance.Id, instance.ProcessName, instance.TotalProcessorTime.TotalMilliseconds, instance.WorkingSet64, instance.MainModule.FileName)); } catch { } } return pInfo; } /// /// 获得特定进程信息 /// /// 进程名称 public List<ProcessInfo> GetProcessInfo(string ProcessName) { List<ProcessInfo> pInfo = new List<ProcessInfo>(); Process[] processes = Process.GetProcessesByName(ProcessName); foreach (Process instance in processes) { try { pInfo.Add(new ProcessInfo(instance.Id, instance.ProcessName, instance.TotalProcessorTime.TotalMilliseconds, instance.WorkingSet64, instance.MainModule.FileName)); } catch { } } return pInfo; } #endregion #region 结束指定进程 /// /// 结束指定进程 /// /// 进程的 Process ID public static void EndProcess(int pid) { try { Process process = Process.GetProcessById(pid); process.Kill(); } catch { } } #endregion #region 查找所有应用程序标题 /// /// 查找所有应用程序标题 /// /// 应用程序标题范型 public static List<string> FindAllApps(int Handle) { List<string> Apps = new List<string>(); int hwCurr; hwCurr = GetWindow(Handle, GW_HWNDFIRST); while (hwCurr > 0) { int IsTask = (WS_VISIBLE | WS_BORDER); int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE); bool TaskWindow = ((lngStyle & IsTask) == IsTask); if (TaskWindow) { int length = GetWindowTextLength(new IntPtr(hwCurr)); StringBuilder sb = new StringBuilder(2 * length + 1); GetWindowText(hwCurr, sb, sb.Capacity); string strTitle = sb.ToString(); if (!string.IsNullOrEmpty(strTitle)) { Apps.Add(strTitle); } } hwCurr = GetWindow(hwCurr, GW_HWNDNEXT); } return Apps; } #endregion } public class DiskInfo { public DiskInfo() { } public DiskInfo(string name, long size, long freeSpace) { this.Name = name; this.Size = size; this.FreeSpace = freeSpace; } public string Name { get; set; } public long Size { get; set; } public long FreeSpace { get; set; } } public class ProcessInfo { public ProcessInfo() { } public ProcessInfo(int id, string processName, double totalMilliseconds, long workingSet64, string fileName) { Id = id; ProcessName = processName; TotalMilliseconds = totalMilliseconds; WorkingSet64 = workingSet64; FileName = fileName; } public int Id { get; set; } public string ProcessName { get; set; } public double TotalMilliseconds { get; set; } public long WorkingSet64 { get; set; } public string FileName { get; set; } } } |
参考:
https://www.cnblogs.com/zery/p/6256850.html
https://www.cnblogs.com/maowang1991/archive/2013/08/27/3285983.html