日期:2014-05-18  浏览次数:20494 次

c#如何获取cup,内存,硬盘的利用率?
如题,有做个这方面的吗?请给一下设计思路最好能提供代码,分不够在加.

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
namespace Windows.Help
{
public partial class SystemInfo : Form
{
public SystemInfo()
{
InitializeComponent();
}

[DllImport( "kernel32 ")]
public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
[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);

//定义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;
}

private void button1_Click(object sender, 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();