Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the how much data is consumed by each application in some duration of time.
What I want, suppose a scenario
time is now 10:00 AM
First time when i captured data.

1. chrome - 1,200,000 Bytes
2. skype - 100,045 Bytes
3. Outlook - 450,612
.
.
.

Suppose After 10 min(10:10 AM)
1. chrome - 2,860,000 Bytes
2. skype - 196,025 Bytes
3. Outlook - 482,214
.
.
.

What I have tried:

C#
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;
        }
    }
}


I don't find any instance on any application. Not in this as well not in category type ".NET CLR Networking 4.0.0.0"
Posted
Comments
gggustafson 24-Jul-17 11:53am    
Have you tried debugging your code? Also in the future please include the using statements for your code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900