码迷,mamicode.com
首页 > Windows程序 > 详细

c# 网速监控

时间:2018-06-07 14:07:32      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:csv   接收   本地计算机   mon   col   ==   --   timer   turn   

技术分享图片
  class Program
    {

        //实例化一个性能计数器,统计网卡流量:发送,字节
        private static System.Diagnostics.PerformanceCounter _sendCounter;

        //实例化一个性能计数器,统计网卡流量:接收,字节
        private static System.Diagnostics.PerformanceCounter _receivedCounter;

        //打印的地址
        private static readonly string filePath = System.IO.Directory.GetCurrentDirectory() + "\\NetworkMonitor.csv";

        static void Main(string[] args)
        {
            Console.Title = "流量监控";

            var adapter = GetNetworkInterface();
            Console.WriteLine("当前连接名称: "+ adapter.Name);
            if (adapter.NetworkInterfaceType.ToString() == "Loopback")
            {
                Console.WriteLine("当前暂无网络连接");
                Console.ReadLine();
                return;
            }
            else
            {
                //实例化一个性能计数器,统计网卡流量:发送,字节
                _sendCounter = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", adapter.Description);

                //实例化一个性能计数器,统计网卡流量:接收,字节
                _receivedCounter = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Received/sec", adapter.Description);

                WriteMessage(filePath, "当前时间,上行,下行");

                var time = ConfigurationManager.AppSettings["Timer"];

                System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(Counter), null,
                    0, Convert.ToInt32(time));

            }

            Console.ReadLine();
        }

        //定时输出
        private static void Counter(object state)
        {

            float sed = _sendCounter.NextValue() / 1024;
            float red = _receivedCounter.NextValue() / 1024;
            StringBuilder str = new StringBuilder();

            str.Append("上行: " + sed.ToString("0.00") + "kbps\r\n");
            str.Append("下行: " + red.ToString("0.00") + "kbps\r\n");
            str.Append("---------------------------------------\r\n");
            Console.WriteLine(str);

            WriteMessage(filePath, DateTime.Now + "," + sed.ToString("0.00") + "," + red.ToString("0.00"));

        }

        /// <summary>
        /// 输出指定信息到文本文件
        /// </summary>
        /// <param name="path">文本文件路径</param>
        /// <param name="msg">输出信息</param>
        public static void WriteMessage(string path, string msg)
        {
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.BaseStream.Seek(0, SeekOrigin.End);
                        sw.WriteLine("{0}", msg, DateTime.Now);
                        sw.Flush();
                    }
                }
            }
            catch (Exception ex)
            {

                Console.WriteLine("写入文件失败: " + ex.Message);
            }

        }


        /// <summary>
        /// 获取本地计算机上网络接口的对象
        /// </summary>
        /// <returns></returns>
        public static NetworkInterface GetNetworkInterface()
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            return adapters.FirstOrDefault(adapter => adapter.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up);
        }
    }
View Code

 

c# 网速监控

标签:csv   接收   本地计算机   mon   col   ==   --   timer   turn   

原文地址:https://www.cnblogs.com/wurong/p/9149829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!