检索过程中的网络使用 [英] Retrieve process network usage
问题描述
我怎样才能得到一个进程发送/接收字节?首选的方法是用C#这样做。
How can I get a process send/receive bytes? the preferred way is doing it with C#.
我已经搜查这个了很多,我没有找到这方面的任何简单的解决方案。一些解决方案建议在计算机上安装WinPcap的以及与此LIB工作
I've searched this a lot and I didn't find any simple solution for this. Some solutions suggested to install the WinPCap on the machine and to work with this lib.
像这个家伙问:的极品"具有网络活动"过程;在托管代码的功能 - 就像resmon.exe做它的
我不希望的lib的开销。
Like this guy asked: Need "Processes with Network Activity" functionality in managed code - Like resmon.exe does it I don't want the overhead of the lib.
有没有一个简单的解决方案?
其实我要的正是数据的Windows资源监视器下的进程与网络活动选项卡为:
Is there a simple solution for this? Actually I want the exactly data that the Resource Monitor of Windows gives under the "Processes with Network Activity" tab:
如何的Windows资源监视器获取这些信息? ?
的任何例子
How does the Resource Monitor of Windows gets this information? Any example?
此外,试图用它在这里提到的计数法:
的缺少网络发送/接收
,但没有成功 - 因为并非每个过程是这样的柜台下图所示。
和再次我想知道资源监视器如何获取此信息,即使不使用此计数器...
Also, tried to use the counter method which is mentioned over here: Missing network sent/received but with no success - as not every process is shown under this counter. And again I'm wondering how the Resource Monitor gets this information even without using this counter...
推荐答案
您可以使用 的PerformanceCounter
。示例代码:
You can use PerformanceCounter
. Sample code:
//Define
string pn = "MyProcessName.exe";
var readOpSec = new PerformanceCounter("Process","IO Read Operations/sec", pn);
var writeOpSec = new PerformanceCounter("Process","IO Write Operations/sec", pn);
var dataOpSec = new PerformanceCounter("Process","IO Data Operations/sec", pn);
var readBytesSec = new PerformanceCounter("Process","IO Read Bytes/sec", pn);
var writeByteSec = new PerformanceCounter("Process","IO Write Bytes/sec", pn);
var dataBytesSec = new PerformanceCounter("Process","IO Data Bytes/sec", pn);
var counters = new List<PerformanceCounter>
{
readOpSec,
writeOpSec,
dataOpSec,
readBytesSec,
writeByteSec,
dataBytesSec
};
// get current value
foreach (PerformanceCounter counter in counters)
{
float rawValue = counter.NextValue();
// display the value
}
这是获得性能计数器网卡。注意它不处理具体
And this is to get performance counters for the Network card. Note it is not process specific
string cn = "get connection string from WMI";
var networkBytesSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", cn);
var networkBytesReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", cn);
var networkBytesTotal = new PerformanceCounter("Network Interface", "Bytes Total/sec", cn);
Counters.Add(networkBytesSent);
Counters.Add(networkBytesReceived);
Counters.Add(networkBytesTotal);
这篇关于检索过程中的网络使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!