作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习如何监控特定应用程序的网络带宽使用情况。我在看 IPv4InterfaceStatistics
,但这似乎可以监控 NIC 卡的性能。
我想监视特定应用程序以查看每秒消耗多少带宽。
有谁知道如何做到这一点的例子?
最佳答案
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;
}
}
}
关于c# - 如何监控特定应用程序的网络带宽使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396786/
我是一名优秀的程序员,十分优秀!