.net中调用windows performance记录性能信息

来源:岁月联盟 编辑:exp 时间:2012-05-28

记录skype的CPU 占用率
 
var processorCounter = new PerformanceCounter
{
       CategoryName = "Process",
       CounterName = "% Processor Time",
       InstanceName = “skype”,
       MachineName = "."
};

processorCounter.NextValue()
 
 
 记录skype的内存占用
 
var memoryCounter = new PerformanceCounter
{
    CategoryName = "Process",
    CounterName = "Working Set - Private",
    InstanceName = “skype”,
    MachineName = "."
};

memoryCounter.RawValue  返回的是字节为计量单位
 
获取当前计算计算机的cpu和内存占用情况
 
counter = new PerformanceCounter("Processor", "% Processor Time", “_total”);

counter.NextValue() cpu占用

ComputerInfo ComputerInfo = new ComputerInfo();

ComputerInfo.TotalPhysicalMemory - ComputerInfo.AvailablePhysicalMemory 内存占用
 
注意, 使用Counter的时候,如果instanceName 不存在, 会出现异常。

 

 

摘自 JustRun1983