http://www.ioage.com/cn/plugins.htm名称:SysState (系统信息)
作者:凤凰工作室
功能:状态栏插件,显示系统信息, 当前网站Ping速度、网卡网络流量、网卡IP地址、当前系统可用物理内存数、当前CPU占用率。
版本:1.0.1.3 发布于2008.8
下载:文件尺寸15KB
就像世界之窗浏览器的这个插件一样,能显示系统信息。主要是CPU和内存。
我非常想知道,C#如何实现这个功能?例如,在Form1里面有一个TextBox1,要在里面显示CPU和内存参数。如何实现?
作者:凤凰工作室
功能:状态栏插件,显示系统信息, 当前网站Ping速度、网卡网络流量、网卡IP地址、当前系统可用物理内存数、当前CPU占用率。
版本:1.0.1.3 发布于2008.8
下载:文件尺寸15KB
就像世界之窗浏览器的这个插件一样,能显示系统信息。主要是CPU和内存。
我非常想知道,C#如何实现这个功能?例如,在Form1里面有一个TextBox1,要在里面显示CPU和内存参数。如何实现?
【转】ASP.NET(c#)中取得当前计算机CPU 内存使用率等相关信息
Posted on 2008-07-18 17:58 coldiceperson 阅读(83) 评论(0) 编辑 收藏 网摘 所属分类: .NET技术文章在Visual C#中调用API的基本过程: 首先,在调用API之前,你必须先导入System.Runtime.InteropServices这个名称空间。该名称空间包含了在Visual C#中调用API的一些必要集合,具体的方法如下: using System.Runtime.InteropServices; 在导入了名称空间后,我们要声明在程序中所要用到的API函数。我们的程序主要是获取系统的相关信息,所以用到的API函数都是返回系统信息的。先给出在Visual C#中声明API的方法:[DllImport("kernel32")] public static extern void GetWindowsDirectory(StringBuilder WinDir,int count); 其中,"DllImport"属性用来从不可控代码中调用一个方法,它指定了DLL的位置,该DLL中包含调用的外部方法;"kernel32"设定了类库名;"public"指明函数的访问类型为公有的;"static"修饰符声明一个静态元素,而该元素属于类型本身而不是指定的对象;"extern"表示该方法将在工程外部执行,同时使用DllImport导入的方法必须使用"extern"修饰符;最后GetWindowsDirectory函数包含了两个参数,一个为StringBuilder类型的,另一个为int类型的,该方法返回的内容存在于StringBuilder类型的参数中。同时,因为我们在这里使用到了StringBuilder类,所以在程序的开始处,我们还得添加System.Text这个名称空间,方法同上。 其他几个API函数的声明如下:[DllImport("kernel32")] public static extern void GetSystemDirectory(StringBuilder SysDir,int count);[DllImport("kernel32")] public static extern void GetSystemInfo(ref CPU_INFO cpuinfo); [DllImport("kernel32")] public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo); [DllImport("kernel32")] public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo); 以上几个API的作用分别是获取系统路径,获得CPU相关信息,获得内存的相关信息,获得系统时间等。在声明完所有的API函数后,我们发现后三个函数分别用到了CPU_INFO、 MEMORY_INFO、SYSTEMTIME_INFO等结构,这些结构并非是.Net内部的,它们从何而来?其实,我们在用到以上API调用时均需用到以上结构,我们将函数调用获得的信息存放在以上的结构体中,最后返回给程序输出。这些结构体比较复杂,但是如果开发者能够熟练运用,那么整个API世界将尽在开发者的掌握之中。以下就是上述结构体的声明://定义以下各结构//定义CPU的信息结构[StructLayout(LayoutKind.Sequential)] public struct CPU_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; }//定义内存的信息结构[StructLayout(LayoutKind.Sequential)] public struct MEMORY_INFO {public uint dwLength;public uint dwMemoryLoad; public uint dwTotalPhys; public uint dwAvailPhys; public uint dwTotalPageFile; public uint dwAvailPageFile; public uint dwTotalVirtual; public uint dwAvailVirtual; }//定义系统时间的信息结构[StructLayout(LayoutKind.Sequential)] public struct SYSTEMTIME_INFO { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds; } 结构体定义的主体部分和C++中的没多大差别,具体每个结构体内部成员的定义可参考联机帮助中的SDK文档。同时,我们还发现在每个结构体定义的上面都有一句用中括号括起来的说明性文字。这些说明都是有关结构体成员的布局的,共有三种选项,分别说明如下: LayoutKind.Automatic:为了提高效率允许运行态对类型成员重新排序。 注意:永远不要使用这个选项来调用不受管辖的动态链接库函数。 LayoutKind.Explicit:对每个域按照FieldOffset属性对类型成员排序 LayoutKind.Sequential:对出现在受管辖类型定义地方的不受管辖内存中的类型成员进行排序。 在上面的程序中,为了方便起见我们都用到了第三种方式所有的API函数以及相关的结构体声明完毕后,我们就运用这些API来实现我们的程序功能――获取系统的相关信息。 界面可按如下方式布置,不过有兴趣的读者自然可以发挥自己的想象,将界面布局做得更好。简单的界面布置好后,我们添加一个按钮("获取信息"按钮)的消息处理函数如下:private void GetInfo_Click(object sender, System.EventArgs e){//调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径const int nChars = 128;StringBuilder Buff = new StringBuilder(nChars);GetWindowsDirectory(Buff,nChars);WindowsDirectory.Text = "Windows路径:"+Buff.ToString();GetSystemDirectory(Buff,nChars);SystemDirectory.Text = "系统路径:"+Buff.ToString();//调用GetSystemInfo函数获取CPU的相关信息CPU_INFO CpuInfo;CpuInfo = new CPU_INFO();GetSystemInfo(ref CpuInfo);NumberOfProcessors.Text = "本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString()+"个CPU";ProcessorType.Text = "CPU的类型为"+CpuInfo.dwProcessorType.ToString();ProcessorLevel.Text = "CPU等级为"+CpuInfo.dwProcessorLevel.ToString();OemId.Text = "CPU的OEM ID为"+CpuInfo.dwOemId.ToString();PageSize.Text = "CPU中的页面大小为"+CpuInfo.dwPageSize.ToString();//调用GlobalMemoryStatus函数获取内存的相关信息MEMORY_INFO MemInfo;MemInfo = new MEMORY_INFO();GlobalMemoryStatus(ref MemInfo);MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用";TotalPhys.Text = "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";AvailPhys.Text = "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";TotalPageFile.Text = "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节";AvailPageFile.Text = "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节";TotalVirtual.Text = "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";AvailVirtual.Text = "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节";//调用GetSystemTime函数获取系统时间信息SYSTEMTIME_INFO StInfo;StInfo = new SYSTEMTIME_INFO();GetSystemTime(ref StInfo);Date.Text = StInfo.wYear.ToString()+"年"+StInfo.wMonth.ToString()+"月"+StInfo.wDay.ToString()+"日";Time.Text = (StInfo.wHour+8).ToString()+"点"+StInfo.wMinute.ToString()+"分"+StInfo.wSecond.ToString()+"秒";}
表示公共信息模型 (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);
} }
}
MSDN Library->Win32 and Com DevelopMent->Administration and Management->Windows Management Instrument->WMI Reference->WMI Classes->Win32 Classes1.获取硬件列表<!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>
http://blog.csdn.net/WYZSC/archive/2008/12/15/3521603.aspx