4

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

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

非常感谢

4

2 回答 2

1

如果您想要当前的下载和上传速度,方法如下:

制作一个间隔为 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";
    }

我想这可以解决它。如果您有不同的计时器时间间隔,只需将您给出的时间除以我们给出的 MAGNITUDE 即可。

于 2013-06-23T09:14:46.610 回答
1

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

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

然后你划分:

double kbsec = kbreceived / secs;

要获得kbreceived,您需要currentBytes读取,添加已读取的字节,然后除以 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;

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

于 2010-02-01T21:26:36.307 回答