我需要一种获取当前系统网络使用情况的方法.
我在网上找到了一些,但是并没有为我工作.
谢谢你的帮助
代码段:
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; } }
解决方法
请参阅
Report information from NetworkInterface: network statistics一个很好的示例程序:
using System; using System.Net.NetworkInformation; class MainClass { static void Main() { if (!NetworkInterface.GetIsNetworkAvailable()) return; NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { Console.WriteLine(" Bytes Sent: {0}",ni.GetIPv4Statistics().BytesSent); Console.WriteLine(" Bytes Received: {0}",ni.GetIPv4Statistics().BytesReceived); } } }