监控特定进程的网络性能

8

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

例如,要获取Explorer的CPU和内存数据,我可以创建如下的性能计数器:

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

很遗憾,Process的categoryName属性不能让我获取该进程的网络使用情况。我可以使用Network Interface的categoryName属性来获取特定网卡上的整体网络使用情况,但无法将其隔离到任何给定的进程中。


看起来之前有类似的问题被问到过:https://dev59.com/P3RC5IYBdhLWcg3wAcXU - Dilshod
2
这是一种架构限制。这些计数器由网络驱动程序堆栈实现。它在内核模式下运行,没有用户模式进程意识。这也是为什么FileSystemWatcher无法告诉您哪个进程修改了文件的相同类型的原因。 - Hans Passant
1个回答

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

使用性能计数器,你只能获取微软允许你访问的值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接