C# 监控进程的网络使用情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/438240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:24:21  来源:igfitidea点击:

Monitor a process's network usage?

c#winapinetwork-monitoring

提问by John T

Is there a way in C# or C/C++ & Win32 to monitor a certain process's network usage (Without that application being built by you obviously)? I would like to monitor just 1 process for like an hour or so, then return the bytes used by only that process, such as limewire for example.

在 C# 或 C/C++ 和 Win32 中有没有办法监视某个进程的网络使用情况(显然该应用程序不是由您构建的)?我只想监控一个进程大约一个小时,然后返回仅由该进程使用的字节,例如limewire。

Is it possible? I know netstat -e on windows will tell you total bytes sent/received, but that is for all processes.

是否可以?我知道 windows 上的 netstat -e 会告诉您发送/接收的总字节数,但这适用于所有进程。

edit: If i can't return just one processes usage, how can i get the bytes sent/received by the entire system? as netstat displays except i just want the integers.

编辑:如果我不能只返回一个进程的使用情况,我怎样才能获得整个系统发送/接收的字节数?作为 netstat 显示,除了我只想要整数。

eg:

例如:

netstat -e

                           Received            Sent

Bytes                    2111568926      1133174989
Unicast packets             3016480         2711006
Non-unicast packets            3122            1100
Discards                          0               0
Errors                            0               0
Unknown protocols                 0

I just want to get 2 variables, like rec = 2111568926 and sent = 1133174989

我只想得到 2 个变量,比如 rec = 2111568926 和 sent = 1133174989

回答by Sam Saffron

You could use WinPcapfor this or write your own device driver. I don't think there are any per process perfmon counters you can leverage.

您可以为此使用WinPcap或编写您自己的设备驱动程序。我不认为您可以利用任何每个进程的性能计数器。

回答by Davy Landman

It's possible, but if I'm not mistaken you'll have to create a network driver to filter all network traffic and than figure out which process created the traffic.

这是可能的,但如果我没记错的话,您必须创建一个网络驱动程序来过滤所有网络流量,然后找出哪个进程创建了流量。

Microsoft has an free application for it called Microsoft Network Monitor 3.2(download). According to the release notes it also has an api to use.

Microsoft 有一个免费的应用程序,称为Microsoft Network Monitor 3.2下载)。根据发行说明,它还有一个 api 可以使用。

Network Monitor API: Create your own applications that capture, parse and analyze network traffic!

网络监视器 API:创建您自己的应用程序来捕获、解析和分析网络流量!

Here is a blog post about these API's.

这是一篇关于这些 API 的博客文章

In my opinion you should use this API (or another API such as WinPcap) to filter the traffic instead of writing your own device driver.

在我看来,您应该使用此 API(或其他 API,例如 WinPcap)来过滤流量,而不是编写您自己的设备驱动程序。

回答by grom

NetLimiter 2 Limiter

NetLimiter 2 限制器

Network Traffic MonitorYou can get the last freeware version from here

网络流量监视器您可以从这里获得最新的免费软件版本

回答by Stanislav Berkov

I asked the same question on MSDN forum and get answer. Frankly speaking have tried it. I.e. tried but not so hard)) It was suggested to use ETW. I did not realize how to consume ETW events properly. http://social.msdn.microsoft.com/Forums/en-US/perfmon/thread/b82da95a-1c18-49ce-9bfa-e3d79ec40907

我在 MSDN 论坛上问了同样的问题并得到了答案。老实说已经试过了。即尝试但不那么努力))有人建议使用ETW。我没有意识到如何正确使用 ETW 事件。http://social.msdn.microsoft.com/Forums/en-US/perfmon/thread/b82da95a-1c18-49ce-9bfa-e3d79ec40907