usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;usingSystem.Collections;usingSystem.Timers;namespaceWindowsFormsApplication2{staticclassProgram{publicpartialclassNetJiankongForm2:Form{publicNetJiankongForm2(){InitializeComponent();}privateMyNetWorkMatchClass[]m_MNWMadapters;privateMyNetWorkMonitormonitor;privatevoidNetJiankongForm2_Load(objectsender,EventArgse){monitor=newMyNetWorkMonitor();m_MNWMadapters=monitor.Adapters;//获得控制器MyNetWorkMonitor上所有计算机的适配器列表if(m_MNWMadapters.Length==0){listBox1.Enabled=false;MessageBox.Show(在计算机上没有找到网络适配器);return;}listBox1.Items.AddRange(m_MNWMadapters);}privatevoidlistBox1_SelectedIndexChanged(objectsender,EventArgse)//选中其中一个适配器{monitor.StopMonitoring();monitor.StartMonitoring(m_MNWMadapters[listBox1.SelectedIndex]);//控制该适配器开始工作this.timer1.Start();//计时开始}privatevoidtimer1_Tick(objectsender,EventArgse)//计时开始,用于每秒钟改变显示速度{MyNetWorkMatchClassadapter=m_MNWMadapters[listBox1.SelectedIndex];//该适配器textBox_Down.Text=String.Format({0:n}kbps,adapter.DownloadSpeedKbps);//得到该适配器的下载速度textBox_Up.Text=String.Format({0:n}kbps,adapter.UploadSpeedKbps);//得到该适配器的上传速度}}publicclassMyNetWorkMonitor{privateSystem.Timers.TimerMonitor_Timer;//计时器事件执行每秒钟刷新值在适配器。privateArrayListm_AdaptersList;//该计算机的适配器列表。privateArrayListm_MonitoredAdapters;//目前控制的适配器列表publicMyNetWorkMonitor(){m_AdaptersList=newArrayList();//用来保存获取到的计算机的适配器列表m_MonitoredAdapters=newArrayList();//运行的有效的适配器列表ShowNetAdapter();//列举出安装在该计算机上面的适配器Monitor_Timer=newSystem.Timers.Timer(1000);Monitor_Timer.Elapsed+=newElapsedEventHandler(timer_ElapsedClick);}privatevoidtimer_ElapsedClick(objectsender,ElapsedEventArgse)//用于每秒钟刷新速度{foreach(MyNetWorkMatchClassadapterinm_MonitoredAdapters)//每秒钟遍历有效的网络适配器{adapter.CaculateAndRefresh();//刷新上传下载速度}}privatevoidShowNetAdapter()//列举出安装在该计算机上面的适配器方法{PerformanceCounterCategoryPCCCategory=newPerformanceCounterCategory(NetworkInterface);foreach(stringInstanceNameinPCCCategory.GetInstanceNames()){if(InstanceName==MSTCPLoopbackinterface)continue;//创建一个实例NetworkAdapter类别,并创建性能计数器它。MyNetWorkMatchClassmyMNWMadapter=newMyNetWorkMatchClass(InstanceName);myMNWMadapter.m_Performance_Down=newPerformanceCounter(NetworkInterface,BytesReceived/sec,InstanceName);myMNWMadapter.m_Performance_Up=newPerformanceCounter(NetworkInterface,BytesSent/sec,InstanceName);m_AdaptersList.Add(myMNWMadapter);}}publicvoidStartMonitoring(){if(m_AdaptersList.Count0){foreach(MyNetWorkMatchClassmyMNWMadapterinm_AdaptersList)if(!m_MonitoredAdapters.Contains(myMNWMadapter)){m_MonitoredAdapters.Add(myMNWMadapter);myMNWMadapter.Start();}Monitor_Timer.Enabled=true;}}publicvoidStartMonitoring(MyNetWorkMatchClassmyMNWMadapter)//控制该适配器开始工作{if(!m_MonitoredAdapters.Contains(myMNWMadapter)){m_MonitoredAdapters.Add(myMNWMadapter);myMNWMadapter.Start();//该适配器调用自己函数开始工作}Monitor_Timer.Enabled=true;}publicvoidStopMonitoring(){m_MonitoredAdapters.Clear();Monitor_Timer.Enabled=false;}publicvoidStopMonitoring(MyNetWorkMatchClassadapter){if(m_MonitoredAdapters.Contains(adapter))m_MonitoredAdapters.Remove(adapter);if(m_MonitoredAdapters.Count==0)Monitor_Timer.Enabled=false;}publicMyNetWorkMatchClass[]Adapters//该控制类所控制的找出所有适配器的适配器列表{get{return(MyNetWorkMatchClass[])m_AdaptersList.ToArray(typeof(MyNetWorkMatchClass));}}}//一个安装在计算机上的网络适配器,该类可用于获取网络中的流量publicclassMyNetWorkMatchClass{publicoverridestringToString(){returnm_strMatchName;}publicstringstrMatchName{get{returnm_strMatchName;}}publiclongDownloadSpeed{get{returnm_lDownLoadSpeed;}}publiclongUploadSpeed{get{returnm_lUpLoadSpeed;}}publicdoubleDownloadSpeedKbps{get{returnthis.m_lUpLoadSpeed/1024.0;}}publicdoubleUploadSpeedKbps{get{returnthis.m_lUpLoadSpeed/1024.0;}}privatelongm_lDownLoadNetValues1;//当前的下载速度,字节计算privatelongm_lUploadNetValues1;//当前的上传速度privatelongm_lDownLoadNetValues2;//一秒前的下载速度,字节计算privatelongm_lUploadNetValues2;//一秒前的上传速度privatestringm_strMatchName;//此适配器的名字internalPerformanceCounterm_Performance_Down;//控制下载速度的流量计算中心internalPerformanceCounterm_Performance_Up;////控制上传速度的流量计算中心privatelongm_lDownLoadSpeed;//每秒钟下载速度privatelongm_lUpLoadSpeed;//每秒钟上传速度publicMyNetWorkMatchClass(stringstrComputerNetName){m_lDownLoadNetValues1=0;m_strMatchName=strComputerNetName;m_lUploadNetValues1=0;m_lDownLoadNetValues2=0;m_lUploadNetValues2=0;}//该适配器准备控制的方法函数publicvoidStart(){m_lUploadNetValues1=m_Performance_Up.NextSample().RawValue;m_lDownLoadNetValues1=m_Performance_Down.NextSample().RawValue;}publicvoidCaculateAndRefresh()//计算速度{m_lDownLoadNetValues2=m_Performance_Down.NextSample().RawValue;m_lUploadNetValues2=m_Performance_Up.NextSample().RawValue;m_lDownLoadSpeed=m_lDownLoadNetValues2-m_lDownLoadNetValues1;m_lUpLoadSpeed=m_lUploadNetValues2-m_lUploadNetValues1;m_lDownLoadNetValues1=m_lDownLoadNetValues2;m_lUploadNetValues1=m_lUploadNetValues2;}}////summary[STAThread]staticvoidMain(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(newForm1());}}}