计算带宽

有没有办法我可以通过网络计算exe /应用程序的带宽(发送和接收的数据包)? 已经陷入了IPGlobalProperties ,

和其他类….我想要一个应用程序收到的数据包发送…我已经检查了http://netstatagent.com/我需要类似的东西…… .net中有什么可以帮助我吗?

我的应用程序连接到Web服务以发送一些图像文件…并且还接收文件…

一种方法是为您的应用程序检索性能计数器 “.NET CLR Networking / Bytes Received”和“.NET CLR Networking / Bytes Sent”的值:

PerformanceCounter bytesSentPerformanceCounter= new PerformanceCounter(); bytesSentPerformanceCounter.CategoryName = ".NET CLR Networking"; bytesSentPerformanceCounter.CounterName = "Bytes Sent"; bytesSentPerformanceCounter.InstanceName = GetInstanceName(); bytesSentPerformanceCounter.ReadOnly = true; float bytesSent = bytesSentPerformanceCounter.NextValue(); //.... private static string GetInstanceName() { // Used Reflector to find the correct formatting: string assemblyName = GetAssemblyName(); if ((assemblyName == null) || (assemblyName.Length == 0)) { assemblyName = AppDomain.CurrentDomain.FriendlyName; } StringBuilder builder = new StringBuilder(assemblyName); for (int i = 0; i < builder.Length; i++) { switch (builder[i]) { case '/': case '\\': case '#': builder[i] = '_'; break; case '(': builder[i] = '['; break; case ')': builder[i] = ']'; break; } } return string.Format(CultureInfo.CurrentCulture, "{0}[{1}]", builder.ToString(), Process.GetCurrentProcess().Id); } private static string GetAssemblyName() { string str = null; Assembly entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) { AssemblyName name = entryAssembly.GetName(); if (name != null) { str = name.Name; } } return str; } 

请注意,性能计数器 直到您第一次使用相关网络库时才会创建 (您将获得InvalidOperation:指定类别中不存在实例'XXX')并且您需要插入

        

在你的app.config中。

有关完整示例,请下载NetworkTraffic.cs和NetworkTraffic.exe.config 。

我记得读过一篇关于此的文章并为你挖出来, http://nayyeri.net/blog/how-to-calculate-network-utilization-in-net/

他们的代码之前的摘录:

.NET带有三个性能计数器,用于开箱即用的网络利用率公式中的使用参数。 所有这些计数器都位于网络接口类别中,并命名为“Bytes Sent / sec”,“Bytes Received / sec”和“Current Bandwidth”。 需要额外努力才能计算的唯一参数是time_in_sec。

“Bytes Sent / sec”和“Bytes Received / sec”计数器根据不同的样本计算它们的值,从这些计数器获得更好值的最佳方法是在循环中找到它们的值的总和,因为在某些情况下它们的值可能是是零或非常不同于网络的真实状态。 然后我们可以通过查找迭代循环的次数来找到time_in_sec参数,因为我们的性能计数器找到它们的值一秒钟,总时间(秒)等于迭代次数。

我查找每个应用程序的字节数/秒…不适用于整个计算机….似乎不适用于控制台应用程序…错误消息:“控制台应用程序不存在于指定的类别中。”

这不起作用……据我所知bytesSentPerformanceCounter.InstanceName =“”//这里你需要提供网卡名称…

不知何故发送的字节太少于收到的字节…它不是我的应用程序浏览网…我发送到Web服务图像(作为字节)和其他XML文件(几个kbs作为字符串输入到Web服务function)。 作为回报,我有时会返回错误代码或bool …仍然发送的字节太少于收到…收到的是5倍…我很困惑……