2013-04-24 58 views
8

我正在编写一个程序,该程序使用.NET性能计数器来获取特定进程的CPU,内存和网络使用情况。监视特定进程的网络性能

例如,要获取CPU和内存数据Explorer,我创建性能计数器是这样的:

PerformanceCounter PC = new PerformanceCounter(); 
PC.CategoryName = "Process"; 
PC.CounterName = "% Processor Time"; 
PC.InstanceName = "Explorer"; 

PerformanceCounter PC = new PerformanceCounter(); 
PC.CategoryName = "Process"; 
PC.CounterName = "Working Set - Private"; 
PC.InstanceName = "Explorer"; 

不幸的是,没有物业的过程categoryName,让我获取该网络使用处理。我可以使用网络接口categoryName来获取任何特定NIC上的整体网络使用情况,但无法将其隔离到任何给定的进程。

+0

它看起来像有类似的问题之前问:http://stackoverflow.com/questions/438240/monitor-a-processs-network-usage – Dilshod 2013-04-25 14:12:10

+2

这是一个体系结构的限制。这些计数器由网络驱动程序堆栈实现。它在内核模式下运行,它不具有用户模式进程意识。 FileSystemWatcher无法告诉您哪个进程修改了文件的同一种原因。 – 2013-04-25 16:46:13

回答

0

我想你只能得到整个系统的网络使用情况。您可以使用此代码:

GetCounterValue(_netRecvCounters[index], "Network Interface", "Bytes Received/sec",  _instanceNames[index]): 

double GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName) 
{ 
    pc.CategoryName = categoryName; 
    pc.CounterName = counterName; 
    pc.InstanceName = instanceName; 
    return pc.NextValue(); 
} 

使用PerformanceCounters,您只能获得Microsoft希望您有权访问的值。