//get operating system
string stringOS = "";
int intWinXP = 0;
queryCollection = getProcessCollection("SELECT * FROM Win32_OperatingSystem");
foreach(ManagementObject mo in queryCollection)
{
//look for Windows 2000 Operating system
stringOS = mo["Caption"].ToString();
intWinXP = stringOS.LastIndexOf("XP");
}
if (intWinXP >= 0)
{
//get process CPU and memory usage for XP machine
//(Note: have problem calling this on 2000 and NT machine.)
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");
}
else
{
//get process CPU and memory usage for 2000 and NT machine
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfRawData_PerfProc_Process");
}
foreach ( ManagementObject mo1 in queryCollection1)
{
//get process CPU and memory usagestring PercentProcessorTime = mo1.Properties["PercentProcessorTime"].Value.ToString();
string VirtualBytes = mo1["VirtualBytes"].ToString();
}
string stringOS = "";
int intWinXP = 0;
queryCollection = getProcessCollection("SELECT * FROM Win32_OperatingSystem");
foreach(ManagementObject mo in queryCollection)
{
//look for Windows 2000 Operating system
stringOS = mo["Caption"].ToString();
intWinXP = stringOS.LastIndexOf("XP");
}
if (intWinXP >= 0)
{
//get process CPU and memory usage for XP machine
//(Note: have problem calling this on 2000 and NT machine.)
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");
}
else
{
//get process CPU and memory usage for 2000 and NT machine
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfRawData_PerfProc_Process");
}
foreach ( ManagementObject mo1 in queryCollection1)
{
//get process CPU and memory usagestring PercentProcessorTime = mo1.Properties["PercentProcessorTime"].Value.ToString();
string VirtualBytes = mo1["VirtualBytes"].ToString();
}
解决方案 »
- FileStream的Read中的offset为什么只能从0开始
- 请教IPAddressCollection和dataset等几个问题?
- 多线程访问窗体控件!
- 谁能帮我解释一下这句话的意思,和一些相关知识,我没这么用过。
- 怎样实现效验自身的md5?
- rdlc报表: 一个dataTable 填充多个表格
- 如何用最快的方法判断TEXTBOX中输入的是数字还是字符?WINFORM
- ----高分求!! 关于COM 对象无效或未注册的问题~~~~~~~~~~
- 100分求,软件注册码生成源码
- 如何在windows程序中的DataGrid 中加入复选框?
- xml:xslt的问题?立即给分
- 请教一下有关搜索引擎的问题
在Form1_Load里加入下面代码:
private void Form1_Load(object sender, System.EventArgs e)
{
const int Movable=2;
const int LocalDisk=3;
const int CD=5;
string type="";
TreeNode node=new TreeNode("我的计算机",3,3);
ManagementObjectSearcher query=new ManagementObjectSearcher();
query.Query=new ObjectQuery("select * from win32_logicaldisk");
ManagementObjectCollection queryCollection=query.Get();
foreach (ManagementObject mo in queryCollection)
{
switch (int.Parse(mo["DriveType"].ToString()))
{
case Movable:
type="移动设备";
break;
case LocalDisk:
type="本地磁盘";
break;
case CD:
type="CD驱动器";
break;
default:
break;
}
TreeNode node1=new TreeNode(type+"("+mo["Name"].ToString()+" 剩余空间:"+mo["FreeSpace"].ToString()+")");
node.Nodes.Add(node1);
}
this.treeView1.Nodes.Add(node);
}
如何获得CPU和内存利用率://get operating system
string stringOS = "";
int intWinXP = 0;
queryCollection = getProcessCollection("SELECT * FROM Win32_OperatingSystem");
foreach(ManagementObject mo in queryCollection)
{
//look for Windows 2000 Operating system
stringOS = mo["Caption"].ToString();
intWinXP = stringOS.LastIndexOf("XP");
}
if (intWinXP >= 0)
{
//get process CPU and memory usage for XP machine
//(Note: have problem calling this on 2000 and NT machine.)
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");
}
else
{
//get process CPU and memory usage for 2000 and NT machine
queryCollection1 = getProcessCollection("SELECT * FROM Win32_PerfRawData_PerfProc_Process");
}
foreach ( ManagementObject mo1 in queryCollection1)
{
//get process CPU and memory usagestring PercentProcessorTime = mo1.Properties["PercentProcessorTime"].Value.ToString();
string VirtualBytes = mo1["VirtualBytes"].ToString();
}
何谓"如何用C#实时获取CPU利用率!"CPU的利用率是什么概念,CPU好像一直在工作啊,没有事情的时候,还在处理idle啊.能不能具体说说想要什么?
using System;
using System.Diagnostics;
using System.Threading;
public class CpuLoadInfo
{ // auxiliary print methods
private static void Say ( string txt )
{
Console.WriteLine(txt);
} // auxiliary print methods
private static void Say()
{
Say("");
} // The main method. Command line arguments are ignored.
[STAThread]
public static void Main()
{
Say("$Id: CpuLoadInfo.cs,v 1.2 2002/08/17 17:45:48 rz65 Exp $");
Say(); Say("Attempt to create a PerformanceCounter instance:");
Say("Category name = " + CategoryName);
Say("Counter name = " + CounterName);
Say("Instance name = " + InstanceName);
PerformanceCounter pc
= new PerformanceCounter(CategoryName,CounterName,InstanceName);
Say("Performance counter was created.");
Say("Property CounterType: " + pc.CounterType);
Say(); Say("Property CounterHelp: " + pc.CounterHelp);
Say();
Say("Entering measurement loop."); while (true)
{
Thread.Sleep(1000); // wait for 1 second
float cpuLoad = pc.NextValue();
Say("CPU load = " + cpuLoad + " %.");
}
} // constants used to select the performance counter.
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total";
}这是在我计算机上的计算结果:
Entering measurement loop.
CPU load = 0 %.
CPU load = 1.941746 %.
CPU load = 4.854369 %.
CPU load = 10 %.
CPU load = 0 %.
CPU load = 2.999997 %.
CPU load = 0.9900987 %.
CPU load = 0 %.