最近因为项目需要,开发的软件设置里面需要获取到当前网路的实时网速,所以在网络上也是搜索了半天,还好找到了一个比较不难理解的
思路是: 获得当前设备的网路适配器的实例,然后利用计时器,不断地获取下载信息,进而计算出当前的网络速度。
首先定义一个适配器的额 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();