//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();
}

解决方案 »

  1.   

    如何获得磁盘剩余空间: 新建一个Windows程序,加入TreeView并添加引用System.Management;
        在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();
    }
      

  2.   

    SELECT * FROM Win32_OperatingSystem怎么那么象SQL语句??学习!
      

  3.   

    getProcessCollection 哪里来的?文中没有提到嘛!
      

  4.   

    密切关注 sdsxc(c#,我为卿狂) ,高人啊
      

  5.   

    可以看一下MSDN中的WMI的介绍及操作!
      

  6.   

    http://www.codeproject.com/useritems/WMI.asp
      

  7.   

    密切关注 sdsxc(c#,我为卿狂)
      

  8.   

    To:楼主,
    何谓"如何用C#实时获取CPU利用率!"CPU的利用率是什么概念,CPU好像一直在工作啊,没有事情的时候,还在处理idle啊.能不能具体说说想要什么?
      

  9.   


    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 %.