溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

獲取機器memory和CPU信息的方法是什么

發(fā)布時間:2020-08-31 14:14:20 來源:億速云 閱讀:186 作者:小新 欄目:編程語言

小編給大家分享一下獲取機器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();

內(nèi)存這塊花了比較多的時間,之前的對象已經(jīng)過期,不能使用了,最后找到這個類“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信息的方法是什么有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI