public partial class NetJiankongForm2 :Form
{
public NetJiankongForm2()
{
InitializeComponent();
}
private MyNetWorkMatchClass[] m_MNWMadapters;
private MyNetWorkMonitor monitor;
private void NetJiankongForm2_Load(object sender, EventArgse)
{
monitor = new MyNetWorkMonitor();
m_MNWMadapters =monitor.Adapters; //获得控制器MyNetWorkMonitor上所有计算机的适配器列表
if (m_MNWMadapters.Length == 0)
{
listBox1.Enabled = false;
MessageBox.Show("在计算机上没有找到网络适配器");
return;
}
listBox1.Items.AddRange(m_MNWMadapters);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgse) //选中其中一个适配器
{
monitor.StopMonitoring();
monitor.StartMonitoring(m_MNWMadapters[listBox1.SelectedIndex]); //控制该适配器开始工作
this.timer1.Start(); //计时开始
}
private void timer1_Tick(object sender, EventArgse) //计时开始,用于每秒钟改变显示速度
{
MyNetWorkMatchClass adapter =m_MNWMadapters[listBox1.SelectedIndex]; //该适配器
textBox_Down.Text = String.Format("{0:n} kbps",adapter.DownloadSpeedKbps); //得到该适配器的下载速度
textBox_Up.Text = String.Format("{0:n} kbps",adapter.UploadSpeedKbps); //得到该适配器的上传速度
}
}
publicclass MyNetWorkMonitor
{
private System.Timers.TimerMonitor_Timer; // 计时器事件执行每秒钟刷新值在适配器。
private ArrayListm_AdaptersList; //该计算机的适配器列表。
private ArrayListm_MonitoredAdapters; //目前控制的适配器列表
public MyNetWorkMonitor()
{
m_AdaptersList = newArrayList(); //用来保存获取到的计算机的适配器列表
m_MonitoredAdapters = newArrayList(); //运行的有效的适配器列表
ShowNetAdapter(); //列举出安装在该计算机上面的适配器
Monitor_Timer = new System.Timers.Timer(1000);
Monitor_Timer.Elapsed += newElapsedEventHandler(timer_ElapsedClick);
}
private void timer_ElapsedClick(object sender, ElapsedEventArgse) //用于每秒钟刷新速度
{
foreach (MyNetWorkMatchClass adapter inm_MonitoredAdapters) //每秒钟遍历有效的网络适配器
{
adapter.CaculateAndRefresh(); //刷新上传下载速度
}
}
private voidShowNetAdapter() //列举出安装在该计算机上面的适配器方法
{
PerformanceCounterCategory PCCCategory = newPerformanceCounterCategory("Network Interface");
foreach (string InstanceName inPCCCategory.GetInstanceNames())
{
if (InstanceName == "MS TCP Loopback interface")
continue;
// 创建一个实例Net workAdapter类别,并创建性能计数器它。
MyNetWorkMatchClass myMNWMadapter = newMyNetWorkMatchClass(InstanceName);
myMNWMadapter.m_Performance_Down = new PerformanceCounter("NetworkInterface", "Bytes Received/sec", InstanceName);
myMNWMadapter.m_Performance_Up = new PerformanceCounter("NetworkInterface", "Bytes Sent/sec", InstanceName);
m_AdaptersList.Add(myMNWMadapter);
}
}
public void StartMonitoring()
{
if (m_AdaptersList.Count > 0)
{
foreach (MyNetWorkMatchClass myMNWMadapter in m_AdaptersList)
if (!m_MonitoredAdapters.Contains(myMNWMadapter))
{
m_MonitoredAdapters.Add(myMNWMadapter);
myMNWMadapter.Start();
}
Monitor_Timer.Enabled = true;
}
}
public void StartMonitoring(MyNetWorkMatchClassmyMNWMadapter) //控制该适配器开始工作
{
if(!m_MonitoredAdapters.Contains(myMNWMadapter))
{
m_MonitoredAdapters.Add(myMNWMadapter);
myMNWMadapter.Start(); //该适配器调用自己函数开始工作
}
Monitor_Timer.Enabled = true;
}
public void StopMonitoring()
{
m_MonitoredAdapters.Clear();
Monitor_Timer.Enabled = false;
}
public void StopMonitoring(MyNetWorkMatchClass adapter)
{
if (m_MonitoredAdapters.Contains(adapter))
m_MonitoredAdapters.Remove(adapter);
if (m_MonitoredAdapters.Count == 0)
Monitor_Timer.Enabled = false;
}
public MyNetWorkMatchClass[]Adapters //该控制类所控制的找出所有适配器的适配器列表
{
get
{
return(MyNetWorkMatchClass[])m_AdaptersList.ToArray(typeof(MyNetWorkMatchClass));
}
}
}
//一个安装在计算机上的网络适配器,该类可用于获取网络中的流量
public classMyNetWorkMatchClass
{
public override string ToString()
{
return m_strMatchName;
}
public string strMatchName
{
get
{
return m_strMatchName;
}
}
public long DownloadSpeed
{
get
{
return m_lDownLoadSpeed;
}
}
public long UploadSpeed
{
get
{
return m_lUpLoadSpeed;
}
}