这个模块的功能也可分为两个小模块,分别是获取网卡列表和对指定的网卡进行流量监控。 (1)获取网卡列表 主要代码如下: monitor = new NetworkMonitor(); if (adapters.Length == 0) {this.listadapters.Enabled = false; return;} this.listadapters.Items.AddRange(this.adapters); (2)监控指定的网卡 if (this.listadapters.SelectedIndex < 0) {MessageBox.Show("请指定要监控的网卡", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.timercounter.Stop(); } else this.timercounter.Start(); 上述代码是防止在没有选择网卡项的情况下而实现的,if语句判断有没有选中指定的网卡,如果没有选择,则this.listadapters.SelectedIndex等于-1,即小于0,反之就会触发计数器对指定网卡进行监控。 NetworkAdapter adapter = this.adapters[this.listadapters.SelectedIndex]; this.down.Text=string.Format("{0:n}kbps",adapter.DownloadSpeedKbps; nsc1.Value = (int)adapter.UploadSpeed / 100; nsc2.Value = (int)adapter.DownloadSpeed / 100; 上面一部分也是通过调用monitor.StopMonitoring()和monitor.StartMonitoring()来实现对列表框中的指定网卡进行监控,中间的命令是当选中指定网卡后开始执行监控,调用计数器,在这个过程中,又通过调用format把NetworkMonitor中的adapter.UploadSpeedKbps(上传流量)和adapter.DownloadSpeedKbps(下载流量)转换成{0:n} kbps格式,并且把流量在波形控件中显示,从而实现对指定网卡的流量监控。 参考文献 [1] 陆晟,龚俭.网络安全监测的集成管理[J].东南大学学报,2009(05). |