获取下载和上传速度 C#
我正在寻找一个类或一个库或任何可以让我获得当前下载速度的东西,我已经尝试了很多来自网络的代码,包括 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你想要 kb/sec。这是通过将
kbreceived
除以当前秒数减去起始秒数来确定的。我不知道如何在 C# 中为此执行 DateTime,但在 VC++ 中,它会像这样:然后除以:
要获得
kbreceived
,您需要获取currentBytes
code> read,添加已读取的字节,然后除以 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:You then divide:
To get
kbreceived
, you need to take thecurrentBytes
read, add in bytes already read, then divide by 1024.So,
Minus some implementation specific functions, the basic concept is the same regardless of language.
如果您想要当前的下载和上传速度,请按以下方法操作:
制作一个间隔为 1 秒的计时器,如果您希望它以该间隔更新,您可以选择。
在计时器滴答声中,添加以下代码:
我想这可以解决问题。
如果您为计时器设置了不同的时间间隔,只需将您给出的时间除以
我们给出的幅度。
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 :
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.
修复上述解决方案:
使用
using System.Net.NetworkInformation;
写下:
使用此答案上面的解决方案大约有 5 个错误,但是我可以通过将定义更改为长整型而不是整数并将小数转换为 Math.Round() 中的双精度来修复它。 > 部分。
如果您遇到性能问题,我建议您使用
BackgroundWorker
控件。Fix for the solution above:
Use
using System.Net.NetworkInformation;
Write Down:
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.