成都网站建设设计

将想法与焦点和您一起共享

获取机器memory和CPU信息的方法是什么

小编给大家分享一下获取机器memory和CPU信息的方法是什么,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为兴文企业提供专业的成都网站制作、网站建设,兴文网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

1. Use WMI to create connection to the computer passing username and password. Once the connection is created,  query the CPU& memory by passing the query, similar as SQL.  This way can get CPU & memory for remote PC and local PC. For example:

System.Management.ConnectionOptions Conn = new ConnectionOptions();

Conn.Username = mpusername;

Conn.Password = mppwd;

string scopestring = "//" + mpserver + "/root/cimv2";

System.Management.ManagementScope Ms = new ManagementScope(scopestring);

Ms.Connect();

mos.Scope = Ms;

ObjectQuery oq = new ObjectQuery();

oq.QueryString = "select * from Win32_Processor";

mos.Query = oq;

ManagementObjectCollection moc = mos.Get();

内存这块花了比较多的时间,之前的对象已经过期,不能使用了,最后找到这个类“Win32_PerfRawData_PerfOS_Memory”

ManagementObjectCollection mcr = mcp.getQueryResult("select * from Win32_ComputerSystem");
           
           foreach (ManagementObject mo in mcr)
           {
               if (mo["TotalPhysicalMemory"] != null)
               {
                   totalm = long.Parse(mo["TotalPhysicalMemory"].ToString());
               }
           }
           ManagementObjectCollection moc = mcp.getQueryResult("select * from Win32_PerfRawData_PerfOS_Memory");
           foreach (ManagementObject mo in moc)
           {
               string avilable = mo.GetPropertyValue("AvailableBytes").ToString();
               avilablem = long.Parse(avilable);
           }

2. Get local server’s CPU and momory information passing the WMI classes, such as “Win32_Processor”, “Win32_OperatingSystem”

ManagementClass mc = new ManagementClass("Win32_OperatingSystem");

ManagementObjectCollection moc = mc.GetInstances();

3. Use performance counter to get performance data passing the performance counter name, such as monitor.PerformanceCounterFun("Processor", "_Total", "% Processor Time"). Normally we shoud get performance counter data several times, then use the average values.

看完了这篇文章,相信你对获取机器memory和CPU信息的方法是什么有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


本文题目:获取机器memory和CPU信息的方法是什么
链接地址:http://chengdu.cdxwcx.cn/article/pioiji.html