码迷,mamicode.com
首页 > Windows程序 > 详细

c#获取电脑运行状态(cpu,内存,网络,系统运行时间)

时间:2018-08-09 21:13:52      阅读:662      评论:0      收藏:0      [点我收藏+]

标签:mat   运行时间   ota   form   foreach   static   com   tin   ret   

 public class DeviceMonitor
    {

        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 static bool GetInternetAvilable()
        {
            bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
            return networkUp;
        }

        public static TimeSpan GetSystemUpTime()
        {
            uptime.NextValue();
            TimeSpan ts = TimeSpan.FromSeconds(uptime.NextValue());
            return ts;
        }

        public static 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;
        }

        public static string getCurrentCpuUsage()
        {
            return cpuCounter.NextValue() + "%";
        }

        public static string getAvailableRAM()
        {
            return ramCounter.NextValue() + "MB";
        }
    }

  

c#获取电脑运行状态(cpu,内存,网络,系统运行时间)

标签:mat   运行时间   ota   form   foreach   static   com   tin   ret   

原文地址:https://www.cnblogs.com/chlm/p/9451498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!