C# 怎么获取CPU使用率、可用内存等

 我来答
匿名用户
2013-06-29
展开全部
public class CpuInfoCollector
{
[DllImport("kernel32")]
//获取系统内存信息
private static extern void GlobalMemoryStatus(ref MemoryInfor meminfo);
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total"; //最大内存使用
private float m_MaxMemory=0;
//最大内存使用时间
private DateTime m_MaxMemoryTime=DateTime.Now;
//最小内存使用
private float m_MinMemory;
//最小内存使用时间
private DateTime m_MinMemoryTime = DateTime.Now; //最大Cpu使用
private float m_MaxCpu=0;
//最大Cpu使用时间
private DateTime m_MaxCpuTime = DateTime.Now;
//最小Cpu使用
private float m_MinCpu;
//最小Cpu使用时间
private DateTime m_MinCpuTime = DateTime.Now; /// <summary>
/// 机器性能类(获取CPU使用率)
/// </summary>
private static PerformanceCounter pc;
public DTServerInfoCollector()
{
pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
}
/// <summary>
/// 获取进程名称
/// </summary>
private static string m_ProcessName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
/// 获取CPU占用率
public float CpuLoad
{
get
{
//实时cpu信息
float cpu = pc.NextValue();
if (cpu>MaxCpu)
{
//最大cpu使用赋值
MaxCpu = cpu;
//最大cpu使用时间
MaxCpuTime = DateTime.Now;
}
if (cpu < MinCpu)
{
//最小cpu使用赋值
MinCpu = cpu;
//最小cpu使用时间
MinCpuTime = DateTime.Now;
}
Thread.Sleep(500);
return pc.NextValue();
}
} //最大内存使用
public float MaxMemory
{
get { return m_MaxMemory; }
set { m_MaxMemory = value; }
} //最大内存使用时间
public DateTime MaxMemoryTime
{
get { return m_MaxMemoryTime; }
set { m_MaxMemoryTime = value; }
}
//最小内存使用
public float MinMemory
{
get { return m_MinMemory; }
set { m_MinMemory = value; }
}
//最小内存使用时间
public DateTime MinMemoryTime
{
get { return m_MinMemoryTime; }
set { m_MinMemoryTime = value; }
}
//最大Cpu使用
public float MaxCpu
{
get { return m_MaxCpu; }
set { m_MaxCpu = value; }
} //最大Cpu使用时间
public DateTime MaxCpuTime
{
get { return m_MaxCpuTime; }
set { m_MaxCpuTime = value; }
}
//最小Cpu使用
public float MinCpu
{
get { return m_MinCpu; }
set { m_MinCpu = value; }
}
//最小Cpu使用时间
public DateTime MinCpuTime
{
get { return m_MinCpuTime; }
set { m_MinCpuTime = value; }
}
/// 获取内存使用
private MemoryInfo GetMemoryInfo()
{
MemoryInfo mInfo = new MemoryInfo();
try
{
MemoryInfor memInfor = GetMemoryInfor();

long lMemoryTotalMB = memInfor.dwTotalPhys/1024/1024;
long lMemoryAvailMB = memInfor.dwAvailPhys/1024/1024;
//已经使用内存=最大内存-剩余内存
long lMemoryUsedMB = lMemoryTotalMB - lMemoryAvailMB;
//最大内存
mInfo.Memory = lMemoryTotalMB;
if (lMemoryUsedMB>MaxMemory)
{
MaxMemory = lMemoryUsedMB;
MaxMemoryTime = DateTime.Now;
}
if (lMemoryUsedMB < MinMemory)
{
MinMemory = lMemoryUsedMB;
MinMemoryTime = DateTime.Now;
}
//已经使用内存
mInfo.MemoryUsed = lMemoryUsedMB;
//最大使用内存
mInfo.MaxMemory = MaxMemory;
//最大内存的时间
mInfo.MaxMemoryTime = MaxMemoryTime;
//最小使用内存
mInfo.MinMemory = MinMemory;
//最小内存的时间
mInfo.MinMemoryTime = MinMemoryTime; }
catch (Exception ex)
{
return null;
}
return mInfo; }
/// <summary>
/// 获取系统内存信息
/// </summary>
private MemoryInfor GetMemoryInfor()
{
MemoryInfor memoryInfo = new MemoryInfor();
GlobalMemoryStatus(ref memoryInfo);
return memoryInfo;
}
/// <summary>
/// 获取程序的线程数
/// </summary>
public static int Threads
{
get
{
int i = 0;
try
{
foreach (Process vProcess in Process.GetProcesses())
{
if (vProcess.ProcessName == m_ProcessName)
{
i = vProcess.Threads.Count;
break;
}
}
}
catch (Exception ex)
{
}
return i;
}
}
}
/// <summary>
/// 定义内存的信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MemoryInfor
{

public uint dwLength;
/// <summary>
/// 已经使用的内存
/// </summary>
public uint dwMemoryLoad;
/// <summary>
/// 总物理内存大小
/// </summary>
public uint dwTotalPhys;
/// <summary>
/// 可用物理内存大小
/// </summary>
public uint dwAvailPhys;
/// <summary>
/// 交换文件总大小
/// </summary>
public uint dwTotalPageFile;
/// <summary>
/// 可用交换文件大小
/// </summary>
public uint dwAvailPageFile;
/// <summary>
/// 总虚拟内存大小
/// </summary>
public uint dwTotalVirtual;
/// <summary>
/// 可用虚拟内存大小
/// </summary>
public uint dwAvailVirtual;
}
匿名用户
推荐于2019-02-10
展开全部
第一步,创建性能监视器对象:
PerformanceCounter _oPerformanceCounter=new PerformanceCounter("Processor","% Processor Time","_Total");
第二步,获取CPU使用情况:
float _nVal=_oPerformanceCounter.NextValue();
_nVal中就是当前CPU的使用率了,加上百分号(%)就是使用率的百分比,比如:
string _s="当前CPU使用率:" + nVal.ToString("0.0") + "%";

Process [] pro;
pro = Process.GetProcesses();
int total=0;
Process temp;
int i;
for(i=0;i<pro.Length ;i++)
{
temp =pro[i];
total=temp.PrivateMemorySize +total ;
}

获得内存的占用大小
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-06-29
展开全部
//cpu频率

using Microsoft.Win32;

private int GetCPUFrequency()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rk.GetValue("~MHz");
int CPUFrequency = (int)obj;
return CPUFrequency;
}
//////////////////////////////////

//磁盘空间 Management

using System.Management;

private long GetFreeDiskSpace()
{
ManagementObject disk = new ManagementObject(
"win32_logicaldisk.deviceid=\"d:\"");
disk.Get();
string totalByte = disk["FreeSpace"].ToString();
long freeDiskSpaceMb = Convert.ToInt64(totalbyte)/1024/1024;
return freeDiskSpaceMb;
}

/////////////////////
//内存信息

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
/**//// <summary>
/// Summary description for Class1.
/// </summary>

class Class1
{
[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;
}
[DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);

public static int Main(string[] args)
{
Class1 class1 = new Class1();

class1.GetMemoryStatus();
return 0;
}
private void GetMemoryStatus()
{
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);

long totalMb = Convert.ToInt64( MemInfo.dwTotalPhys.ToString())/1024/1024;
long avaliableMb = Convert.ToInt64( MemInfo.dwAvailPhys.ToString())/1024/1024;

Console.WriteLine( "物理内存共有" + totalMb + " MB");
Console.WriteLine( "可使用的物理内存有" + avaliableMb +" MB");
}

}
//////////////////////////////

//cpu名字

using Microsoft.Win32;
private string GetCPUName()
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor\0");

object obj = rk.GetValue("ProcessorNameString");
string CPUName = (string)obj;
return CPUName.TrimStart();
}

///////////////////////
//OS版本

using System;

namespace determineOS_CS
{
class Class1
{
static void Main(string[] args)
{
// Get OperatingSystem information from the system namespace.
System.OperatingSystem osInfo =System.Environment.OSVersion;

// Determine the platform.
switch(osInfo.Platform)
{
// Platform is Windows 95, Windows 98,
// Windows 98 Second Edition, or Windows Me.
case System.PlatformID.Win32Windows:

switch (osInfo.Version.Minor)
{
case 0:
Console.WriteLine ("Windows 95");
break;
case 10:
if(osInfo.Version.Revision.ToString()=="2222A")
Console.WriteLine("Windows 98 Second Edition");
else
Console.WriteLine("Windows 98");
break;
case 90:
Console.WriteLine("Windows Me");
break;
}
break;

// Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000,
// or Windows XP.
case System.PlatformID.Win32NT:

switch(osInfo.Version.Major)

{
case 3:
Console.WriteLine("Windows NT 3.51");
break;
case 4:
Console.WriteLine("Windows NT 4.0");
break;
case 5:
if (osInfo.Version.Minor==0)
Console.WriteLine("Windows 2000");
else
Console.WriteLine("Windows XP");
break;
}break;
}
Console.ReadLine ();
}
}
} 嘿嘿
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
生沙弥暨dJ
2017-06-19 · 超过14用户采纳过TA的回答
知道答主
回答量:37
采纳率:0%
帮助的人:9.5万
展开全部
去工作管理员看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式