您好,登錄后才能下訂單哦!
小編給大家分享一下獲取機器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信息的方法是什么有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。