最近因為項目需要,開發的軟件設置里面需要獲取到當前網路的實時網速,所以在網絡上也是搜索了半天,還好找到了一個比較不難理解的
思路是: 獲得當前設備的網路適配器的實例,然后利用計時器,不斷地獲取下載信息,進而計算出當前的網絡速度。
首先定義一個適配器的額 list 集合,創建一個適配器類,
public class NetworkAdapter { internal NetworkAdapter(string name) { this.name = name; } private long dlSpeed, ulSpeed; // Download/Upload speed in bytes per second. private long dlValue, ulValue; // Download/Upload counter value in bytes. private long dlValueOld, ulValueOld; // Download/Upload counter value one second earlier, in bytes. internal string name; // The name of the adapter. internal PerformanceCounter dlCounter, ulCounter; // Performance counters to monitor download and upload speed. /// <summary> /// Preparations for monitoring. /// </summary> internal void init() { // Since dlValueOld and ulValueOld are used in method refresh() to calculate network speed, they must have be initialized. this.dlValueOld = this.dlCounter.NextSample().RawValue; this.ulValueOld = this.ulCounter.NextSample().RawValue; } /// <summary> /// Obtain new sample from performance counters, and refresh the values saved in dlSpeed, ulSpeed, etc. /// This method is supposed to be called only in NetworkMonitor, one time every second. /// </summary> internal void refresh() { this.dlValue = this.dlCounter.NextSample().RawValue; this.ulValue = this.ulCounter.NextSample().RawValue; // Calculates download and upload speed. this.dlSpeed = this.dlValue - this.dlValueOld; this.ulSpeed = this.ulValue - this.ulValueOld; this.dlValueOld = this.dlValue; this.ulValueOld = this.ulValue; } /// <summary> /// Overrides method to return the name of the adapter. /// </summary> /// <returns>The name of the adapter.</returns> public override string ToString() { return this.name; } /// <summary> /// The name of the network adapter. /// </summary> public string Name { get { return this.name; } } /// <summary> /// Current download speed in bytes per second. /// </summary> public long DownloadSpeed { get { return this.dlSpeed; } } /// <summary> /// Current upload speed in bytes per second. /// </summary> public long UploadSpeed { get { return this.ulSpeed; } } /// <summary> /// Current download speed in kbytes per second. /// </summary> public double DownloadSpeedKbps { get { return this.dlSpeed / 1024.0; } } /// <summary> /// Current upload speed in kbytes per second. /// </summary> public double UploadSpeedKbps { get { return this.ulSpeed / 1024.0; } } }
在使用的地方定義一個計時器用來獲取信息
初始化獲得適配器實例
this.adapters = new ArrayList(); PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface"); foreach (string name in category.GetInstanceNames()) { // This one exists on every computer. if (name == "MS TCP Loopback interface" || name.Contains("isatap") || name.Contains("Interface")) continue; // Create an instance of NetworkAdapter class, and create performance counters for it. NetworkAdapter adapter = new NetworkAdapter(name); adapter.dlCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", name); adapter.ulCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", name); this.adapters.Add(adapter); // Add it to ArrayList adapter } workAdapter = (NetworkAdapter)adapters[0]; workAdapter.init(); dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(timer_Tick); dispatcherTimer.Interval = TimeSpan.FromSeconds(1); dispatcherTimer.Start();