最近作項目須要用到監測網速及流量,我通過百度和牆內谷歌都沒能快速發現監測IPV6流量和網速的用例;也通過本身的一番查詢和調試,浪費了很多時間,如今做爲經驗分享出來但願你們指正。c#
using System.Net.NetworkInformation; using System.Timers; namespace Monitor { public class MonitorNetwork { public string UpSpeed { get; set; } public string DownSpeed { get; set; } public string AllTraffic { get; set; } private string NetCardDescription { get; set; } //創建鏈接時上傳的數據量 private long BaseTraffic { get; set; } private long OldUp { get; set; } private long OldDown { get; set; } private NetworkInterface networkInterface { get; set; } private Timer timer = new Timer() { Interval = 1000 }; public void Close() { timer.Stop(); } public MonitorNetwork(string netCardDescription) { timer.Elapsed += Timer_Elapsed; NetCardDescription = netCardDescription; timer.Interval = 1000; } public bool Start() { networkInterface = null; NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (var var in nics) { if (var.Description.Contains(NetCardDescription)) { networkInterface = var; break; } } if (networkInterface == null) { return false; } else { BaseTraffic = (networkInterface.GetIPStatistics().BytesSent + networkInterface.GetIPStatistics().BytesReceived); OldUp = networkInterface.GetIPStatistics().BytesSent; OldDown = networkInterface.GetIPStatistics().BytesReceived; timer.Start(); return true; } } private string[] units = new string[] {"KB/s","MB/s","GB/s" }; private void CalcUpSpeed() { long nowValue = networkInterface.GetIPStatistics().BytesSent; int num = 0; double value = (nowValue - OldUp) / 1024.0; while (value > 1023) { value = (value / 1024.0); num++; } UpSpeed = value.ToString("0.0") + units[num]; OldUp = nowValue; } private void CalcDownSpeed() { long nowValue = networkInterface.GetIPStatistics().BytesReceived; int num = 0; double value = (nowValue - OldDown) / 1024.0; while (value > 1023) { value = (value / 1024.0); num++; } DownSpeed = value.ToString("0.0") + units[num]; OldDown = nowValue; } private string[] unitAlls = new string[] { "KB", "MB", "GB" ,"TB"}; private void CalcAllTraffic() { long nowValue = OldDown+OldUp; int num = 0; double value = (nowValue- BaseTraffic) / 1024.0; while (value > 1023) { value = (value / 1024.0); num++; } AllTraffic = value.ToString("0.0") + unitAlls[num]; } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { CalcUpSpeed(); CalcDownSpeed(); CalcAllTraffic(); } } }
雖然沒能直接快速地百度到方法,可是實現這個需求的時候,內心是有個譜,Windows系統能監測到這個網速和流量,沒理由實現不了,只須要一個方法將這個信息讀取出來就好。最後實現這個需求是利用了System.Net.NetworkInformation這個程序集,可是這個程序集沒有隻接提供網速監測的方法,而是提供了接收和發送數據量的屬性,須要本身計算出即便網速,因此這個網速不是特別的準確。api
這個程序集其實一開始就看到了,前輩方法中使用的是IPv4InterfaceStatistics類中的BytesReceived屬性和BytesSent屬性實現的,可是在這個程序集裏沒有對應的IPv6類,恍恍惚惚。測試
而後呢,我就下意識覺得這個程序集比較老舊,不支持IPv6統計信息讀取,而後也是各類搜索無果,以後呢不死心想再來研究研究,東點點西瞅瞅,而後在NetworkInterface 類中發現了一個GetIPStatistics()方法,它的描述是「獲取此 NetworkInterface 實例的 IP 統計信息。」。spa
而後就瓜熟蒂落的事了,根據GetIPStatistics()返回的IPInterfaceStatistics實例中的BytesReceived屬性和BytesSent屬性就能獲取到收發的數據總量,而後根據這個信息就能計算出大約的網速。.net
經測試,利用IPInterfaceStatistics實例是能讀取到IPv4和IPv6的總數據量的,由於此次的需求就是監測總量,若是須要單獨監測IPv6的能夠用總量減去IPv4部分。調試
老師之前喊我認真唸書,我心想有百度還不夠嗎,再念能有百度聰明,有百度懂得多,後來漸漸明白,百度懂得多都是前輩的搬磚添瓦來的,共勉。code