获取网速

获取网络传输速度 

使用PerformanceCounter类(System.Diagnostics)

1、源码:

     class NETSpeed
      {
        //网络发送速度
        public float NetTrafficSend { get; set; }
        //网络接收速度
        public float NetTrafficReceive { get; set; }



        private PerformanceCounterCategory performanceNetCounterCategory;

        private PerformanceCounter[] trafficSentCounters;

        private PerformanceCounter[] trafficReceivedCounters;
        //用于记录性能对象实例名称
        private string[] interfaces = null;

        public void InitNetCounters()
        {

            performanceNetCounterCategory = new PerformanceCounterCategory("Network Interface");
            //获取性能对象实例名称
            interfaces = performanceNetCounterCategory.GetInstanceNames();

            int length = interfaces.Length;

            if (length > 0)

            {

                trafficSentCounters = new PerformanceCounter[length];

                trafficReceivedCounters = new PerformanceCounter[length];

            }

            for (int i = 0; i < length; i++)

            {

                trafficReceivedCounters[i] = new PerformanceCounter("Network Interface", "Bytes Received/sec", interfaces[i]);

                trafficSentCounters[i] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", interfaces[i]);

            }

        }

        /// <summary>
        /// 网络上传速度
        /// </summary>
        public void GetCurretTrafficSent()

        {

            int length = interfaces.Length;

            float sendSum = 0.0F;
            for (int i = 0; i < length; i++)
            {
                 //获取上传数据量
                float temp = trafficSentCounters[i].NextValue();
                //第一次获取值为0的处理方法
                if (i== 0 && temp ==0)
                {
                    Thread.Sleep(500);
                    temp = trafficSentCounters[i].NextValue();
                }
                sendSum += temp;

            }
            float tmp = (sendSum / 1024);

            NetTrafficSend = (float)(Math.Round((double)tmp, 1));

        }
        /// <summary>
        /// 网络下载速度
        /// </summary>
        public void GetCurrentTrafficReceived()
        {
            int length = interfaces.Length;

            float receiveSum = 0.0F;

            for (int i = 0; i < length; i++)
            {
                //获取下载数据量
                float temp = trafficReceivedCounters[i].NextValue();
                if (i== 0 && temp ==0)
                {
                    Thread.Sleep(500);
                    temp = trafficReceivedCounters[i].NextValue();
                }
                receiveSum += temp;
            }

            float tmp = (receiveSum / 1024);

            NetTrafficReceive = (float)(Math.Round((double)tmp, 1));

        }

   }

使用方法:将上述内容保存为.cs类 

调用方法:

            //实例化对象 
            NETSpeed netSpeed = new NETSpeed();
            //初始化
            netSpeed.InitNetCounters();
            //获取上传速度
            netSpeed.GetCurretTrafficSent();
            float netTrafficSent = netSpeed.NetTrafficSend;
            Console.WriteLine("网络上传速度:" + netTrafficSent + "KB/s");
            //获取下载速度
            netSpeed.GetCurrentTrafficReceived();
            float netTrafficReceive = netSpeed.NetTrafficReceive;
            Console.WriteLine("网络下载速度:" + netTrafficReceive + "KB/s");

以上方法获取的为一秒内的网速

2、运行结果:

 

对比任务管理器:(数据不是相同时刻,此处只为对比)

3、注意事项:

1、注意单位的不同,代码处获取的单位为KB/S  任务管理器的为Kb/s     两者相差8倍

2、文章来源与实践,存在问题请留言!

3、转载请标注来源,谢谢您的阅读!

发布了35 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wenming111/article/details/103909536