温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-31 14:14:20 来源:亿速云 阅读:179 作者:小新 栏目:编程语言

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

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信息的方法是什么有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI