4

I want to write a method in C# to check which applications in my machine/server are using internet connection at a particular point in time and if possible, how much bandwidth they are using. Can anyone please help me get a head start on this?

casperOne
  • 73,706
  • 19
  • 184
  • 253
samar
  • 5,021
  • 9
  • 47
  • 71
  • 1
    This may help --> http://stackoverflow.com/questions/566139/detecting-network-connection-speed-and-bandwidth-usage-in-c – digEmAll Jan 03 '11 at 10:57
  • 1
    Also, have a look at this q&a: http://stackoverflow.com/questions/442409/c-bandwidth – digEmAll Jan 03 '11 at 11:26
  • Thanks @dig for the insight. I think this will be a useful start. You can convert this into an answer so that i can mark it as one. – samar Jan 03 '11 at 13:57
  • I don't deserve any point, because I've just linked an other q&a. You should rather think to upvote that q&a, and if you think it's equal to this one, you should close your question :-) – digEmAll Jan 03 '11 at 14:48
  • Yeah I have upvoted the question..!! :-) Just 1 question here. The links you have provided shows how can we check the internet usage in general. In addition to that I want to find the currently running applications in my machine which are using the internet and also if possible get how much bandwidth each of the application is using. Is this possible? – samar Jan 03 '11 at 15:19
  • @samar Please read the faq `Please look around to see if your question has already been asked (and maybe even answered!) before you ask.` digEmAll has already pointed you to your answer http://stackoverflow.com/questions/566139/detecting-network-connection-speed-and-bandwidth-usage-in-c see 2nd answer. If you have already tried the sample code update your question with the error you get : ) – Searock Jan 03 '11 at 16:36
  • @samar: I've just noticed that the solution I linked, works only for .NET processes. There's another similar question here (http://stackoverflow.com/questions/630285/programmatically-getting-per-process-network-statistics-on-windows), but the accepted answer is not a simple solution... – digEmAll Jan 03 '11 at 18:25
  • @dig i figured that the solution is not that straight. And I am not such an expert in software engineering to understand the TDI filter driver and stuffs like that although I am more than willing to learn it. Think you can guide me to the appopriate sites/books/code which will help me understand this? – samar Jan 04 '11 at 06:20

3 Answers3

3

I decided to write an answer because comments are too small.

Well, reading other Q&A on stackoverflow and looking around on the internet, I didn't find a simple solution for your problem.

Actually, for .NET processes is really simple, you just need to retrieve informations from ".NET CLR Networking - Bytes Received/Bytes Sent" performance counters, as shown in this Q&A

But in general, getting per-process used bandwidth isn't an easy work.

For example "Microsoft Network Monitor" sniffer can trace the process that generates internet packets only for TCP traffic, because probably it maps IP-port pairs with processes using them (or something similar, TCP is a connected protocol so it is simpler).
Anyway if you want to give it a try you can use the exposed API (look at this blog entry for some hint).

However, as suggested in these Q&A's (LINK 1, LINK 2), the right, and probably the only way, is to write a NDIS/TDI driver that can intercept network traffic and exposing a .NET callable API to it.

The problem is that such drivers can't be written in managed code, and so you need to implement it in C/C++.
Obviously, if you manage to find an already written driver/sniffer exposing a callable API, you can use it.
For example WinPCap has one (and some .NET wrappers like SharpPCap or PCap.Net), but I don't think (not sure) it's able to get packets's source-process information.

Community
  • 1
  • 1
digEmAll
  • 56,430
  • 9
  • 115
  • 140
0

As digEmAll noted, in pre-Vista Windows you are reduced to writing your own driver or using a 3-rd party one. In Vista, 2008 and Windows 7 you can use the GetPerTcpConnectionEStats API (there is a large example of its usage on the MSDN page). Resource Monitor relies on this API, together with the older GetTcpTable/GetTcpRow APIs, for extended network statistics.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
0

I found Process Monitor as a very useful tool and it served my purpose so I didnt had to write any code although i am yet to check out whether it gives any API which i can use in my application to get some information I need.

Thanks everyone for helping me out.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
samar
  • 5,021
  • 9
  • 47
  • 71