如何在C#中显示每秒接收的字节数 [英] How to display bytes received per second in C#

查看:304
本文介绍了如何在C#中显示每秒接收的字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每秒显示接收到的字节,并每秒将它们显示在控制台中一次.一旦达到.nextvalue,我将收到一个无效的操作异常.

I'm trying to display the received bytes per second and have them displayed in the console once a second. The I am presented with a Invalid Operation Exception once it reaches .nextvalue.

PerformanceCounter NetworkDownSpeed = new PerformanceCounter("Network Interface", "Bytes Received/sec");
float CurrentNetworkDownSpeed = (int)NetworkDownSpeed.NextValue();

while (true)
{
    Console.WriteLine("Current Network Download Speed: {0}MB", CurrentNetworkDownSpeed);
    Thread.Sleep(1000);
}

推荐答案

很抱歉在某人回答后回答,但这是否有帮助?

Sorry for answering after someone already did, but does this helps by any chance ?

private static void ShowNetworkTraffic()
{
    PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
    string instance = performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
    PerformanceCounter performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
    PerformanceCounter performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);

    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("bytes sent: {0}k\tbytes received: {1}k", performanceCounterSent.NextValue() / 1024, performanceCounterReceived.NextValue() / 1024);
        Thread.Sleep(500);
    }
}

此处.

这篇关于如何在C#中显示每秒接收的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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