8

I'm writing a program that uses the .NET performance counter to get CPU, Memory and Network usage for a specific process.

For example, to get the CPU and Memory data for Explorer, I create performance counters like this:

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";

Unfortunately, there is no property for the Process categoryName that allows me to get network usage for that process. I can use the Network Interface categoryName to get overall network usage on any particular NIC, but cannot isolate it to any given Process.

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
  • it looks like there is something similar asked before:http://stackoverflow.com/questions/438240/monitor-a-processs-network-usage – Dilshod Apr 25 '13 at 14:12
  • 2
    This is an architectural limitation. These counters are implemented by the network driver stack. Which runs in kernel mode, it doesn't have usermode process awareness. Same kind of reason why FileSystemWatcher cannot tell you which process modified a file. – Hans Passant Apr 25 '13 at 16:46

1 Answers1

0

I suppose you can only get the network usage for the entire system. You can use this code:

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();  
}

With PerformanceCounters, you can only get the values that Microsoft want's you to have access to.