using System; using System.Diagnostics; using System.Management; using System.Text; namespace HDLCloudMonitor.InfoMonitor { /// /// windows api 名称 /// public enum WindowsAPIType { /// /// 内存 /// Win32_PhysicalMemory, /// /// cpu /// Win32_Processor, /// /// 硬盘 /// win32_DiskDrive, /// /// 电脑型号 /// Win32_ComputerSystemProduct, /// /// 分辨率 /// Win32_DesktopMonitor, /// /// 显卡 /// Win32_VideoController, /// /// 操作系统 /// Win32_OperatingSystem } public enum WindowsAPIKeys { /// /// 名称 /// Name, /// /// 显卡芯片 /// VideoProcessor, /// /// 显存大小 /// AdapterRAM, /// /// 分辨率宽 /// ScreenWidth, /// /// 分辨率高 /// ScreenHeight, /// /// 电脑型号 /// Version, /// /// 硬盘容量 /// Size, /// /// 内存容量 /// Capacity, /// /// cpu核心数 /// NumberOfCores } /// /// 电脑信息类 单例 /// public class Computer { public static Computer _instance; private static readonly object _lock = new object(); static readonly PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); static readonly PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes"); static readonly PerformanceCounter uptime = new PerformanceCounter("System", "System Up Time"); public Computer() { } public static Computer CreateComputer() { if (_instance == null) { lock (_lock) { if (_instance == null) { _instance = new Computer(); } } } return _instance; } /// /// 当前物理内存使用率 /// /// public string GetPhysicalMemory() { string str = null; ManagementObjectSearcher objCS = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"); foreach (ManagementObject objMgmt in objCS.Get()) { str = objMgmt["totalphysicalmemory"].ToString(); } return str; } /// /// 当前CPU状态 /// /// public int getCurrentCpuUsage() { return (int)cpuCounter.NextValue(); } /// /// 内存 /// /// public string getAvailableRAM() { var a = ramCounter.RawValue; var b = ramCounter.NextValue(); var c = ramCounter.NextSample().RawValue; return b + "MB"; } public int GetMemoryRunStatus() { //获取总物理内存大小 ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory"); ManagementObjectCollection moc1 = cimobject1.GetInstances(); double available = 0, capacity = 0; foreach (ManagementObject mo1 in moc1) { capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1))); } moc1.Dispose(); cimobject1.Dispose(); //获取内存可用大小 ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory"); ManagementObjectCollection moc2 = cimobject2.GetInstances(); foreach (ManagementObject mo2 in moc2) { available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1))); } moc2.Dispose(); cimobject2.Dispose(); return (int)(Math.Round((capacity - available) / capacity * 100, 0)); } /// /// 获取系统内存大小 /// /// 内存大小(单位M) public string GetPhisicalMemory() { ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象 searcher.Query = new SelectQuery(WindowsAPIType.Win32_PhysicalMemory.ToString(), "", new string[] { WindowsAPIKeys.Capacity.ToString() });//设置查询条件 ManagementObjectCollection collection = searcher.Get(); //获取内存容量 ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator(); long capacity = 0; while (em.MoveNext()) { ManagementBaseObject baseObj = em.Current; if (baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value != null) { try { capacity += long.Parse(baseObj.Properties[WindowsAPIKeys.Capacity.ToString()].Value.ToString()); } catch { return "查询失败"; } } } return ToGB((double)capacity, 1024.0); } /// /// 将字节转换为GB /// /// 字节值 /// 除数,硬盘除以1000,内存除以1024 /// public static string ToGB(double size, double mod) { String[] units = new String[] { "B", "KB", "MB", "GB", "TB", "PB" }; int i = 0; while (size >= mod) { size /= mod; i++; } return Math.Round(size) + units[i]; } } }