一只单身猿


JinMo Ye
open_in_new

C#网络监测类

alarm2016-01-19 faceyejinmo

using System.Timers;
using System.Collections;
using System.Diagnostics;
namespace NetWorkSpeedMonitor
{
    /// <summary>  
    /// The NetworkMonitor class monitors network speed for each network adapter on the computer,  
    /// using classes for Performance counter in .NET library.  
    /// </summary>  
    public class NetworkMonitor
    {
        private Timer timer;                // 为网络适配器刷新值的定时器
        private ArrayList adapters;         // 此计算机上的网络适配器列表 
        private ArrayList monitoredAdapters;// 当前被监视的网络适配器列表

        public NetworkMonitor()
        {
            this.adapters = new ArrayList();
            this.monitoredAdapters = new ArrayList();
            EnumerateNetworkAdapters();

            timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }
        /// <summary>  
        /// 枚举安装在此计算机上的网络适配器。
        /// </summary>  
        private void EnumerateNetworkAdapters()
        {
            // 请勿修改此函数内部字符串
            PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");

            foreach (string name in category.GetInstanceNames())
            {
                // 每台计算机上都存在此部分
                if (name == "MS TCP Loopback interface")
                    continue;
                // 创建网络适配器类的实例并为其创建性能计数器
                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); // 添加至数组
            }
        }

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            foreach (NetworkAdapter adapter in this.monitoredAdapters)
                adapter.refresh();
        }
        /// <summary>  
        /// 获取此计算机上已安装的网络适配器实例。
        /// </summary>  
        public NetworkAdapter[] Adapters
        {
            get
            {
                return (NetworkAdapter[])this.adapters.ToArray(typeof(NetworkAdapter));
            }
        }
        /// <summary>  
        /// Enable the timer and add all adapters to the monitoredAdapters list,   
        /// unless the adapters list is empty.  
        /// </summary>  
        public void StartMonitoring()
        {
            if (this.adapters.Count > 0)
            {
                foreach (NetworkAdapter adapter in this.adapters)
                    if (!this.monitoredAdapters.Contains(adapter))
                    {
                        this.monitoredAdapters.Add(adapter);
                        adapter.init();
                    }

                timer.Enabled = true;
            }
        }
        /// <summary>  
        /// Enable the timer, and add the specified adapter to the monitoredAdapters list  
        /// </summary>  
        public void StartMonitoring(NetworkAdapter adapter)
        {
            if (!this.monitoredAdapters.Contains(adapter))
            {
                this.monitoredAdapters.Add(adapter);
                adapter.init();
            }
            timer.Enabled = true;
        }
        /// <summary>  
        /// Disable the timer, and clear the monitoredAdapters list.  
        /// </summary>  
        public void StopMonitoring()
        {
            this.monitoredAdapters.Clear();
            timer.Enabled = false;
        }
        /// <summary>  
        /// Remove the specified adapter from the monitoredAdapters list, and   
        /// disable the timer if the monitoredAdapters list is empty.  
        /// </summary>  
        public void StopMonitoring(NetworkAdapter adapter)
        {
            if (this.monitoredAdapters.Contains(adapter))
                this.monitoredAdapters.Remove(adapter);
            if (this.monitoredAdapters.Count == 0)
                timer.Enabled = false;
        }
    }
}

This is way better than a brick & mortar esmthlisbtena.

2016-05-23 07:43

不错的文章,内容无懈可击.禁止此消息:nolinkok@163.com

2016-06-03 12:19


发表评论