CSDN-CSDN社区-.NET技术-C#

收藏 C#软件里,如何显示CPU和内存参数?[问题点数:90,结帖人:dvdvip]

  • dvdvip
  • 等 级:
  • 结帖率:
楼主发表于:2009-01-09 21:58:39
http://www.ioage.com/cn/plugins.htm

名称:SysState (系统信息)
作者:凤凰工作室
功能:状态栏插件,显示系统信息, 当前网站Ping速度、网卡网络流量、网卡IP地址、当前系统可用物理内存数、当前CPU占用率。
版本:1.0.1.3 发布于2008.8
下载:文件尺寸15KB




就像世界之窗浏览器的这个插件一样,能显示系统信息。主要是CPU和内存。


我非常想知道,C#如何实现这个功能?例如,在Form1里面有一个TextBox1,要在里面显示CPU和内存参数。如何实现?
回复次数:5
#1楼 得分:10回复于:2009-01-09 22:03:19
up
#2楼 得分:35回复于:2009-01-09 22:04:30
#3楼 得分:10回复于:2009-01-09 22:12:59
ManagementClass 类
表示公共信息模型 (CIM) 管理类。管理类是一个 WMI 类,如 Win32_LogicalDisk 和 Win32_Process,前者表示磁盘驱动器,后者表示进程(如 Notepad.exe)。通过该类的成员,可以使用特定的 WMI 类路径访问 WMI 数据。有关更多信息,请参见“Windows Management Instrumentation”文档中的“Win32 Classes”(Win32 类),该文档位于 http://www.microsoft.com/china/msdn/library 上的 MSDN Library 中。
using System;
using System.Management;

public class Sample
{   
    public static void Main()
    {

        // Get the WMI class
        ManagementClass c = new ManagementClass(
            new ManagementPath("Win32_LogicalDisk"));

        // Get the methods in the class
        MethodDataCollection methods =
            c.Methods;

        // display the methods
        Console.WriteLine("Method Names: ");
        foreach (MethodData method in methods)
        {
            Console.WriteLine(method.Name);
        }
        Console.WriteLine();

        // Get the properties in the class
        PropertyDataCollection properties =
            c.Properties;

        // display the properties
        Console.WriteLine("Property Names: ");
        foreach (PropertyData property in properties)
        {
            Console.WriteLine(property.Name);
        }
        Console.WriteLine();

        // Get the Qualifiers in the class
        QualifierDataCollection qualifiers =
            c.Qualifiers;

        // display the qualifiers
        Console.WriteLine("Qualifier Names: ");
        foreach (QualifierData qualifier in qualifiers)
        {
            Console.WriteLine(qualifier.Name);
        }

    }
}
#4楼 得分:5回复于:2009-01-09 22:23:32
要获取更多客户端硬件信息可以参考:
MSDN Library->Win32 and Com DevelopMent->Administration and Management->Windows Management Instrument->WMI Reference->WMI Classes->Win32 Classes

1.获取硬件列表

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
      <head>
            <title> </title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1">
            <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
      <script id=clientEventHandlersJS language=javascript>
<!--

function Button1_onclick() {
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM
Win32_OnBoardDevice");
      var e = new Enumerator (properties);
      document.write(" <table border=1>");
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
            document.write(" <tr>");
            document.write(" <td>" + p.Description + " </td>");
            document.write(" <td>" + p.DeviceType + " </td>");
            document.write(" <td>" + p.Enabled + " </td>");
            document.write(" <td>" + p.Tag + " </td>");
            document.write(" </tr>");
      }
      document.write(" </table>");
}

//-->
</script>
</head>
      <body>
            <INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()">
      </body>
</html>


2.Javascript获取CPU信息


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
      <head>
            <title> </title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1">
            <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
      <script id=clientEventHandlersJS language=javascript>
<!--

function Button1_onclick() {
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM Win32_Processor");
      var e = new Enumerator (properties);
      document.write(" <table border=1>");
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
            document.write(" <tr>");
            document.write(" <td>" + p.Caption + " </td>");
            document.write(" <td>" + p.DeviceID + " </td>");
            document.write(" <td>" + p.Name + " </td>");
            document.write(" <td>" + p.CpuStatus + " </td>");
            document.write(" <td>" + p.Availability + " </td>");
            document.write(" <td>" + p.Level + " </td>");
            document.write(" <td>" + p.ProcessorID + " </td>");
            document.write(" <td>" + p.SystemName + " </td>");
            document.write(" <td>" + p.ProcessorType + " </td>");
            document.write(" </tr>");
      }
      document.write(" </table>");
}

//-->
</script>
</head>
      <body>
            <INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()">
      </body>
</html>


3.Javascript获取主板信息


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
      <head>
            <title> </title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1">
            <meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
      <script id=clientEventHandlersJS language=javascript>
<!--

function Button1_onclick() {
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard");
      var e = new Enumerator (properties);
      document.write(" <table border=1>");
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
            document.write(" <tr>");
            document.write(" <td>" + p.HostingBoard + " </td>");
            document.write(" <td>" + p.Manufacturer + " </td>");
            document.write(" <td>" + p.PoweredOn + " </td>");
            document.write(" <td>" + p.Product + " </td>");
            document.write(" <td>" + p.SerialNumber + " </td>");
            document.write(" <td>" + p.Version + " </td>");
            document.write(" </tr>");
      }
      document.write(" </table>");
}

//-->
</script>
</head>
      <body>
            <INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()">
      </body>
</html>
  • wangping_li用户头像
  • wangping_li
  • (总有一天,我星球上的人会来接我)
  • 等 级:
#5楼 得分:30回复于:2009-01-10 02:01:17