1 public class DeviceMonitor 2 { 3 4 static readonly PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); 5 static readonly PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes"); 6 static readonly PerformanceCounter uptime = new PerformanceCounter("System", "System Up Time"); 7 8 9 public static bool GetInternetAvilable() 10 { 11 bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); 12 return networkUp; 13 } 14 15 public static TimeSpan GetSystemUpTime() 16 { 17 uptime.NextValue(); 18 TimeSpan ts = TimeSpan.FromSeconds(uptime.NextValue()); 19 return ts; 20 } 21 22 public static string GetPhysicalMemory() 23 { 24 string str = null; 25 ManagementObjectSearcher objCS = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem"); 26 foreach (ManagementObject objMgmt in objCS.Get()) 27 { 28 str = objMgmt["totalphysicalmemory"].ToString(); 29 } 30 return str; 31 } 32 33 public static string getCurrentCpuUsage() 34 { 35 return cpuCounter.NextValue() + "%"; 36 } 37 38 public static string getAvailableRAM() 39 { 40 return ramCounter.NextValue() + "MB"; 41 } 42 }
c# 监测电脑状态,CPU使用率,物理内存使用,开机时间,网络状态