C# 获取系统网络使用情况
我需要一种方法来获取当前系统网络使用情况(上下)。
我在网上找到了一些,但它们不适合我。
感谢您的帮助
代码片段:
private void timerPerf_Tick(object sender, EventArgs e)
{
if (!NetworkInterface.GetIsNetworkAvailable())
return;
NetworkInterface[] interfaces
= NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
this.labelnetup.Text = " Bytes Sent: " + ni.GetIPv4Statistics().BytesSent;
this.labelnetdown.Text = " Bytes Rec: " + ni.GetIPv4Statistics().BytesReceived;
}
}
I need a way to get the current system network usage, up and down.
I found some on the net but they're not working out for me.
Thanks for your help
Code snippet:
private void timerPerf_Tick(object sender, EventArgs e)
{
if (!NetworkInterface.GetIsNetworkAvailable())
return;
NetworkInterface[] interfaces
= NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
this.labelnetup.Text = " Bytes Sent: " + ni.GetIPv4Statistics().BytesSent;
this.labelnetdown.Text = " Bytes Rec: " + ni.GetIPv4Statistics().BytesReceived;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅来自NetworkInterface的报告信息:网络统计信息以获得一个好的示例程序:
Please see Report information from NetworkInterface: network statistics for a good sample program:
您也可以通过使用 PerformanceCounterCategory 来获取当前系统的网络使用情况:
来源:http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580
You can get the current system's network usage by working with PerformanceCounterCategory too:
Source: http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580
这是带有计时器的 Windows 窗体的代码。
Here is the code for a windows form with timer.
我想添加另一个答案,因为我假设我不是唯一一个想要以 MBit/s 为单位确定 badnwith 而不仅仅是字节的人:
应该从线程调用此函数,以免阻塞 GUI 一秒钟
I would like to add another answer because I assume I am not the only one who would like to determine the badnwith in MBit/s rather than just bytes:
this function should be called from a thread to not block the GUI for a second