获取下载和上传速度 C#

发布于 2024-08-19 18:11:39 字数 121 浏览 4 评论 0原文

我正在寻找一个类或一个库或任何可以让我获得当前下载速度的东西,我已经尝试了很多来自网络的代码,包括 FreeMeter,但无法让它工作。

有些人可以提供任何类型的代码来提供这个简单的功能吗?

多谢

I'm looking for a class or a library or anything that will allow me to get the current download speed, I've tried a lot of code from the net including FreeMeter but can't get it to work.

Can some provide any sort of code just to give this simple functionality.

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尛丟丟 2024-08-26 18:11:39

我猜你想要 kb/sec。这是通过将 kbreceived 除以当前秒数减去起始秒数来确定的。我不知道如何在 C# 中为此执行 DateTime,但在 VC++ 中,它会像这样:

COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime()
                           - dlStart;
secs = dlElapsed.GetTotalSeconds();

然后除以:

double kbsec = kbreceived / secs;

要获得 kbreceived,您需要获取 currentBytes code> read,添加已读取的字节,然后除以 1024。

因此,

   // chunk size 512.. could be higher up to you

   while (int bytesread = file->Read(charBuf, 512))
   {
        currentbytes = currentbytes + bytesread;
        // Set progress position by setting pos to currentbytes
   }



   int percent = currentbytes * 100 / x ( our file size integer
                               from above);
   int kbreceived = currentbytes / 1024;

减去一些特定的实现函数,无论语言如何,基本概念都是相同的。

I'm guessing you want kb/sec. That is determined by taking kbreceived and dividing it by the current seconds minus the starting seconds. I'm not sure how to do the DateTime for this in C#, but in VC++ it would be like so:

COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime()
                           - dlStart;
secs = dlElapsed.GetTotalSeconds();

You then divide:

double kbsec = kbreceived / secs;

To get kbreceived, you need to take the currentBytes read, add in bytes already read, then divide by 1024.

So,

   // chunk size 512.. could be higher up to you

   while (int bytesread = file->Read(charBuf, 512))
   {
        currentbytes = currentbytes + bytesread;
        // Set progress position by setting pos to currentbytes
   }



   int percent = currentbytes * 100 / x ( our file size integer
                               from above);
   int kbreceived = currentbytes / 1024;

Minus some implementation specific functions, the basic concept is the same regardless of language.

森林散布 2024-08-26 18:11:39

如果您想要当前的下载和上传速度,请按以下方法操作:

制作一个间隔为 1 秒的计时器,如果您希望它以该间隔更新,您可以选择。
在计时器滴答声中,添加以下代码:

using System.Net.NetworkInformation;

int previousbytessend = 0;
int previousbytesreceived = 0;
int downloadspeed;
int uploadspeed;
IPv4InterfaceStatistics interfaceStats;
private void timer1_Tick(object sender, EventArgs e)
    {

        //Must Initialize it each second to update values;
        interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

        //SPEED = MAGNITUDE / TIME ; HERE, TIME = 1 second Hence :
        uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; //In KB/s
        downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

        previousbytessend= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
        previousbytesreceived= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

        downloadspeedlabel.Text = Math.Round(downloadspeed, 2) + " KB/s"; //Rounding to 2 decimal places
        uploadspeedlabel.Text = Math.Round(uploadspeed, 2) + "KB/s";
    }

我想这可以解决问题。
如果您为计时器设置了不同的时间间隔,只需将您给出的时间除以
我们给出的幅度。

If you want Current Download and Upload Speed, here is how :

Make a timer of interval 1 second, if you want it to update at that interval, your choice.
On the timers tick, add this code :

using System.Net.NetworkInformation;

int previousbytessend = 0;
int previousbytesreceived = 0;
int downloadspeed;
int uploadspeed;
IPv4InterfaceStatistics interfaceStats;
private void timer1_Tick(object sender, EventArgs e)
    {

        //Must Initialize it each second to update values;
        interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

        //SPEED = MAGNITUDE / TIME ; HERE, TIME = 1 second Hence :
        uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; //In KB/s
        downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

        previousbytessend= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
        previousbytesreceived= NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

        downloadspeedlabel.Text = Math.Round(downloadspeed, 2) + " KB/s"; //Rounding to 2 decimal places
        uploadspeedlabel.Text = Math.Round(uploadspeed, 2) + "KB/s";
    }

I guess that solves it.
If you have different time interval for the timer, just divide the time you gave with
the MAGNITUDE we have given.

少钕鈤記 2024-08-26 18:11:39

修复上述解决方案:

  1. 使用 using System.Net.NetworkInformation;

  2. 写下:

        long previousbytessend = 0;
        long previousbytesreceived = 0;
        long downloadspeed;
        long uploadspeed;
        IPv4InterfaceStatistics interfaceStats;

        private void internetSpeed_Tick(object sender, EventArgs e)
        {
            interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

            uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; 
            downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

            previousbytessend = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
            previousbytesreceived = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

            txtDownloaded.Text = Math.Round(((double)downloadspeed), 2) + " KB/s";
            txtUploaded.Text = Math.Round(((double)uploadspeed), 2) + "KB/s";
        }

使用此答案上面的解决方案大约有 5 个错误,但是我可以通过将定义更改为长整型而不是整数并将小数转换为 Math.Round() 中的双精度来修复它。 > 部分。

如果您遇到性能问题,我建议您使用 BackgroundWorker 控件。

Fix for the solution above:

  1. Use using System.Net.NetworkInformation;

  2. Write Down:

        long previousbytessend = 0;
        long previousbytesreceived = 0;
        long downloadspeed;
        long uploadspeed;
        IPv4InterfaceStatistics interfaceStats;

        private void internetSpeed_Tick(object sender, EventArgs e)
        {
            interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

            uploadspeed = (interfaceStats.BytesSent - previousbytessend) / 1024; 
            downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

            previousbytessend = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesSent;
            previousbytesreceived = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

            txtDownloaded.Text = Math.Round(((double)downloadspeed), 2) + " KB/s";
            txtUploaded.Text = Math.Round(((double)uploadspeed), 2) + "KB/s";
        }

There were about 5 errors using the solution above this answer, however I was able to fix it by changing the definitions to longs instead of ints and converting the decimals into doubles in the Math.Round() part.

If you are having problems with Performance, I'd recommend you use the BackgroundWorker control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文