用一个timer,然后每隔一时间,把系统状况读出,显示就可了

解决方案 »

  1.   

    我知道用timer,如何读取系统状况?怎样以windows任务管理器的方式显示cpu占用率等信息?
      

  2.   

    //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();
    }
    如何获得磁盘剩余空间: 新建一个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();
    }如何获得磁盘剩余空间: 新建一个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();
    }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 %.
      

  3.   

    声明:
    以上代码是TheAres写的
    我只是看你用得着才帮着贴上来的
      

  4.   

    谢谢wangjingjing390(晶晶) 和幕后英雄TheAres。