代码改变世界

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

2012-05-27 15:26  JustRun  阅读(466)  评论(1编辑  收藏  举报

记录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 不存在, 会出现异常。