如何监视特定应用的网络带宽的使用? [英] How to monitor the network bandwidth usage of a specific application?

查看:374
本文介绍了如何监视特定应用的网络带宽的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何监视特定应用的网络带宽的使用。我在看 IPv4InterfaceStatistics ,但似乎监视NIC卡的性能。结果
我想监视特定的应用程序,看看有多少带宽消耗每一秒。结果
有谁知道如何可以做到这一点的例子吗?


解决方案

 使用系统;使用System.Diagnostics程序
;
使用System.Globalization;
:使用System.IO;使用System.Net
;使用的System.Net.Sockets
;
使用的System.Reflection;
使用System.Text;
使用的System.Threading;

命名空间ConsoleApplication2
{
类节目
{
静态无效的主要(字串[] args)
{
,而(真)
{
变种bytesSentPerformanceCounter =新的PerformanceCounter();
bytesSentPerformanceCounter.CategoryName =.NET CLR网络;
bytesSentPerformanceCounter.CounterName =发送的字节数;
bytesSentPerformanceCounter.InstanceName = GetInstanceName();
bytesSentPerformanceCounter.ReadOnly = TRUE;

变种bytesReceivedPerformanceCounter =新的PerformanceCounter();
bytesReceivedPerformanceCounter.CategoryName =.NET CLR网络;
bytesReceivedPerformanceCounter.CounterName =接收的字节;
bytesReceivedPerformanceCounter.InstanceName = GetInstanceName();
bytesReceivedPerformanceCounter.ReadOnly = TRUE;

Console.WriteLine(发送的字节数:{0},bytesSentPerformanceCounter.RawValue);
Console.WriteLine(接收的字节数:{0},bytesReceivedPerformanceCounter.RawValue);
Thread.sleep代码(1000);
}
}

私人静态字符串GetInstanceName()
{
字符串返回值=未找到;
//检查带宽使用CUPC.exe..Change它与您的应用程序名称
字符串的applicationName =CUPC;
PerformanceCounterCategory []数组= PerformanceCounterCategory.GetCategories();
的for(int i = 0; I< Array.Length;我++)
{
如果(阵列[我] .CategoryName.Contains(NET CLR联网。))
的foreach(在阵列VAR项[I] .GetInstanceNames())
{
如果(item.ToLower()。包含(applicationName.ToString()。ToLower将()))
返回值=项目;

}

}
返回返回值;
}
}
}


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?

解决方案

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

这篇关于如何监视特定应用的网络带宽的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆