主要有两个类,其一是NetworkAdapter,该类的作用是获取本机网络适配器列表,并且可以通过该类的属性获取当前网速数据;其二是NetworkMonitor,该类是通过.NET的PerformanceCounter(性能计数器组件)监测本机每一个网络适配器对应的当前网速状况(翻译得不大好,具体还是看原汁原味的代码吧:))
NetworkAdapter类
- using System;
- using System.Diagnostics;
- namespace NetWorkSpeedMonitor
- {
- /// <summary>
- /// Represents a network adapter installed on the machine.
- /// Properties of this class can be used to obtain current network speed.
- /// </summary>
- public class NetworkAdapter
- {
- /// <summary>
- /// Instances of this class are supposed to be created only in an NetworkMonitor.
- /// </summary>
- 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; }
- }
- }
- }
NetworkMonitor类
Form关键代码
- using NetWorkSpeedMonitor;
- private NetworkAdapter[] adapters;
- private NetworkMonitor monitor;
- private void FormMain_Load(object sender, System.EventArgs e)
- {
- monitor = new NetworkMonitor();
- this.adapters = monitor.Adapters;
- /* If the length of adapters is zero, then no instance
- * exists in the networking category of performance console.*/
- if (adapters.Length == 0)
- {
- this.ListAdapters.Enabled = false;
- MessageBox.Show("No network adapters found on this computer.");
- return;
- }
- this.ListAdapters.Items.AddRange(this.adapters);
- }
- private void ListAdapters_SelectedIndexChanged(object sender, System.EventArgs e)
- {
- monitor.StopMonitoring();
- // Start a timer to obtain new performance counter sample every second.
- monitor.StartMonitoring(adapters[this.ListAdapters.SelectedIndex]);
- this.TimerCounter.Start();
- }
- private void TimerCounter_Tick(object sender, System.EventArgs e)
- {
- NetworkAdapter adapter = this.adapters[this.ListAdapters.SelectedIndex];
- /* The DownloadSpeedKbps and UploadSpeedKbps are double values. You can also
- * use properties DownloadSpeed and UploadSpeed, which are long values but
- * are measured in bytes per second. */
- this.LableDownloadValue.Text = String.Format("{0:n} kbps", adapter.DownloadSpeedKbps);
- this.LabelUploadValue.Text = String.Format("{0:n} kbps", adapter.UploadSpeedKbps);
- }
运行效果:
原文地址:http://www.codeproject.com/KB/system/networkmonitorl.aspx