I am trying to learn how to monitor the network bandwidth usage of a specific application. I am looking at IPv4InterfaceStatistics
, but that seems to monitor an NIC card's performance.
I'd like to monitor a specific application to see how much bandwidth is consumed every second.
Does anyone know of an example of how this can be done?
Asked
Active
Viewed 1.2k times
5

Nathan Tuggy
- 2,237
- 27
- 30
- 38

Manish Jain
- 1,197
- 1
- 11
- 32
-
Resource Monitor for Windows. – Deepak Mishra Dec 10 '14 at 09:00
-
1@DeepakMishra I asume he wants to resolve this programatically, since it is C# and stack, not serverfault, but i could be wrong. – Magic-Mouse Dec 10 '14 at 09:05
-
Yes I want to resolve this programatically – Manish Jain Dec 10 '14 at 09:08
-
http://stackoverflow.com/questions/442409/calculating-bandwidth/442459#442459 – Deepak Mishra Dec 10 '14 at 09:15
-
Network Performance Counters http://msdn.microsoft.com/en-us/library/70xadeyt%28v=vs.110%29.aspx – Paul Zahra Dec 10 '14 at 09:19
-
the above link should answer your question.. – Deepak Mishra Dec 10 '14 at 09:20
-
can you please provide me an example @DeepakMishra – Manish Jain Dec 10 '14 at 09:54
-
@Manish: let me know if that doesn't work – Deepak Mishra Dec 11 '14 at 08:51
-
It shows that Instance does not exist in the specified category. – Manish Jain Dec 12 '14 at 08:58
3 Answers
3
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
var bytesSentPerformanceCounter = new PerformanceCounter();
bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesSentPerformanceCounter.CounterName = "Bytes Sent";
bytesSentPerformanceCounter.InstanceName = GetInstanceName();
bytesSentPerformanceCounter.ReadOnly = true;
var bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = GetInstanceName();
bytesReceivedPerformanceCounter.ReadOnly = true;
Console.WriteLine("Bytes sent: {0}", bytesSentPerformanceCounter.RawValue);
Console.WriteLine("Bytes received: {0}", bytesReceivedPerformanceCounter.RawValue);
Thread.Sleep(1000);
}
}
private static string GetInstanceName()
{
string returnvalue = "not found";
//Checks bandwidth usage for CUPC.exe..Change it with your application Name
string applicationName = "CUPC";
PerformanceCounterCategory[] Array = PerformanceCounterCategory.GetCategories();
for (int i = 0; i < Array.Length; i++)
{
if (Array[i].CategoryName.Contains(".NET CLR Networking"))
foreach (var item in Array[i].GetInstanceNames())
{
if (item.ToLower().Contains(applicationName.ToString().ToLower()))
returnvalue = item;
}
}
return returnvalue;
}
}
}

Deepak Mishra
- 2,984
- 1
- 26
- 32
1
Here is a modified version using PerformanceCounter:
var processFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
var bytesReceivedPerformanceCounter = new PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName = ".NET CLR Networking 4.0.0.0";
bytesReceivedPerformanceCounter.CounterName = "Bytes Received";
bytesReceivedPerformanceCounter.InstanceName = VersioningHelper.MakeVersionSafeName(processFileName, ResourceScope.Machine, ResourceScope.AppDomain);
bytesReceivedPerformanceCounter.ReadOnly = true;
Console.WriteLine("Bytes received: {0}", bytesReceivedPerformanceCounter.RawValue);

TQD
- 11
- 1
-2
If you are familiar with the OSI model http://en.wikipedia.org/wiki/OSI_model you see that you try to interact with layer 3 where you should be interacting with layer 7.
I your connection with what ever class you are using you are able to measure bytes sent, specially if you are transporting the individual bytes, (this is since i have no idea how your code is looking), you should be able to calculate the amount of bytes over a time divide with number of seconds and you will have your result.

Magic-Mouse
- 603
- 10
- 21