C#獲取CPU佔用率、記憶體佔用、磁碟佔用、程式資訊
努力的藍精靈發表於2013-10-04
-
using System;
-
using System.Collections.Generic;
-
using System.Diagnostics;
-
using System.Threading;
-
using System.IO;
-
using System.Text;
-
using System.Management;
-
using System.Runtime.InteropServices;
-
-
namespace Lemony.SystemInfo
-
{
-
-
-
-
public class SystemInfo
-
{
-
private int m_ProcessorCount = 0;
-
private PerformanceCounter pcCpuLoad;
-
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 API宣告
-
[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()
-
{
-
-
pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
-
pcCpuLoad.MachineName = ".";
-
pcCpuLoad.NextValue();
-
-
-
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個數
-
-
-
-
public int ProcessorCount
-
{
-
get
-
{
-
return m_ProcessorCount;
-
}
-
}
-
#endregion
-
-
#region CPU佔用率
-
-
-
-
public float CpuLoad
-
{
-
get
-
{
-
return pcCpuLoad.NextValue();
-
}
-
}
-
#endregion
-
-
#region 可用記憶體
-
-
-
-
public long MemoryAvailable
-
{
-
get
-
{
-
long availablebytes = 0;
-
-
-
-
-
-
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 GetLogicalDrives()
-
{
-
List drives = new List();
-
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
-
ManagementObjectCollection disks = diskClass.GetInstances();
-
foreach (ManagementObject disk in disks)
-
{
-
-
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 GetLogicalDrives(char DriverID)
-
{
-
List drives = new List();
-
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 GetProcessInfo()
-
{
-
List pInfo = new List();
-
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 GetProcessInfo(string ProcessName)
-
{
-
List pInfo = new List();
-
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 結束指定程式
-
-
-
-
-
public static void EndProcess(int pid)
-
{
-
try
-
{
-
Process process = Process.GetProcessById(pid);
-
process.Kill();
-
}
-
catch { }
-
}
-
#endregion
-
-
-
#region 查詢所有應用程式標題
-
-
-
-
-
public static List FindAllApps(int Handle)
-
{
-
List Apps = new List();
-
-
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
-
}
-
}