zoukankan      html  css  js  c++  java
  • [转]C#网速监测

     

    C#网速监测

    分类: C# 1458人阅读 评论(0) 收藏 举报

    主要有两个类,其一是NetworkAdapter,该类的作用是获取本机网络适配器列表,并且可以通过该类的属性获取当前网速数据;其二是NetworkMonitor,该类是通过.NET的PerformanceCounter(性能计数器组件)监测本机每一个网络适配器对应的当前网速状况(翻译得不大好,具体还是看原汁原味的代码吧:))

    NetworkAdapter类

    [c-sharp:collapse] view plaincopyprint?
    1. using System;  
    2. using System.Diagnostics;  
    3.   
    4. namespace NetWorkSpeedMonitor  
    5. {  
    6.     /// <summary>  
    7.     /// Represents a network adapter installed on the machine.  
    8.     /// Properties of this class can be used to obtain current network speed.  
    9.     /// </summary>  
    10.     public class NetworkAdapter  
    11.     {  
    12.         /// <summary>  
    13.         /// Instances of this class are supposed to be created only in an NetworkMonitor.  
    14.         /// </summary>  
    15.         internal NetworkAdapter(string name)  
    16.         {  
    17.             this.name = name;  
    18.         }  
    19.   
    20.         private long dlSpeed, ulSpeed;       // Download/Upload speed in bytes per second.  
    21.         private long dlValue, ulValue;       // Download/Upload counter value in bytes.  
    22.         private long dlValueOld, ulValueOld; // Download/Upload counter value one second earlier, in bytes.  
    23.   
    24.         internal string name;                               // The name of the adapter.  
    25.         internal PerformanceCounter dlCounter, ulCounter;   // Performance counters to monitor download and upload speed.  
    26.         /// <summary>  
    27.         /// Preparations for monitoring.  
    28.         /// </summary>  
    29.         internal void init()  
    30.         {  
    31.             // Since dlValueOld and ulValueOld are used in method refresh() to calculate network speed, they must have be initialized.  
    32.             this.dlValueOld = this.dlCounter.NextSample().RawValue;  
    33.             this.ulValueOld = this.ulCounter.NextSample().RawValue;  
    34.         }  
    35.         /// <summary>  
    36.         /// Obtain new sample from performance counters, and refresh the values saved in dlSpeed, ulSpeed, etc.  
    37.         /// This method is supposed to be called only in NetworkMonitor, one time every second.  
    38.         /// </summary>  
    39.         internal void refresh()  
    40.         {  
    41.             this.dlValue = this.dlCounter.NextSample().RawValue;  
    42.             this.ulValue = this.ulCounter.NextSample().RawValue;  
    43.   
    44.             // Calculates download and upload speed.  
    45.             this.dlSpeed = this.dlValue - this.dlValueOld;  
    46.             this.ulSpeed = this.ulValue - this.ulValueOld;  
    47.   
    48.             this.dlValueOld = this.dlValue;  
    49.             this.ulValueOld = this.ulValue;  
    50.         }  
    51.         /// <summary>  
    52.         /// Overrides method to return the name of the adapter.  
    53.         /// </summary>  
    54.         /// <returns>The name of the adapter.</returns>  
    55.         public override string ToString()  
    56.         {  
    57.             return this.name;  
    58.         }  
    59.         /// <summary>  
    60.         /// The name of the network adapter.  
    61.         /// </summary>  
    62.         public string Name  
    63.         {  
    64.             get { return this.name; }  
    65.         }  
    66.         /// <summary>  
    67.         /// Current download speed in bytes per second.  
    68.         /// </summary>  
    69.         public long DownloadSpeed  
    70.         {  
    71.             get { return this.dlSpeed; }  
    72.         }  
    73.         /// <summary>  
    74.         /// Current upload speed in bytes per second.  
    75.         /// </summary>  
    76.         public long UploadSpeed  
    77.         {  
    78.             get { return this.ulSpeed; }  
    79.         }  
    80.         /// <summary>  
    81.         /// Current download speed in kbytes per second.  
    82.         /// </summary>  
    83.         public double DownloadSpeedKbps  
    84.         {  
    85.             get { return this.dlSpeed / 1024.0; }  
    86.         }  
    87.         /// <summary>  
    88.         /// Current upload speed in kbytes per second.  
    89.         /// </summary>  
    90.         public double UploadSpeedKbps  
    91.         {  
    92.             get { return this.ulSpeed / 1024.0; }  
    93.         }  
    94.     }  
    95. }  

    NetworkMonitor类

    Form关键代码

    1. using NetWorkSpeedMonitor;  
    2.   
    3.         private NetworkAdapter[] adapters;  
    4.         private NetworkMonitor monitor;  
    5.   
    6.         private void FormMain_Load(object sender, System.EventArgs e)  
    7.         {  
    8.             monitor = new NetworkMonitor();  
    9.             this.adapters = monitor.Adapters;  
    10.             /* If the length of adapters is zero, then no instance  
    11.              * exists in the networking category of performance console.*/  
    12.             if (adapters.Length == 0)  
    13.             {  
    14.                 this.ListAdapters.Enabled = false;  
    15.                 MessageBox.Show("No network adapters found on this computer.");  
    16.                 return;  
    17.             }  
    18.             this.ListAdapters.Items.AddRange(this.adapters);  
    19.         }  
    20.   
    21.         private void ListAdapters_SelectedIndexChanged(object sender, System.EventArgs e)  
    22.         {  
    23.             monitor.StopMonitoring();  
    24.             // Start a timer to obtain new performance counter sample every second.  
    25.             monitor.StartMonitoring(adapters[this.ListAdapters.SelectedIndex]);  
    26.             this.TimerCounter.Start();  
    27.         }  
    28.   
    29.         private void TimerCounter_Tick(object sender, System.EventArgs e)  
    30.         {  
    31.             NetworkAdapter adapter = this.adapters[this.ListAdapters.SelectedIndex];  
    32.             /* The DownloadSpeedKbps and UploadSpeedKbps are double values. You can also  
    33.              * use properties DownloadSpeed and UploadSpeed, which are long values but  
    34.              * are measured in bytes per second. */  
    35.             this.LableDownloadValue.Text = String.Format("{0:n} kbps", adapter.DownloadSpeedKbps);  
    36.             this.LabelUploadValue.Text = String.Format("{0:n} kbps", adapter.UploadSpeedKbps);  
    37.         }  

    运行效果:

    原文地址:http://www.codeproject.com/KB/system/networkmonitorl.aspx

  • 相关阅读:
    为什么转速环经过pi调节后输出的是电流?
    如何将mysql、php、nginx设置为开机自启动
    rm-rf /*
    2020/4/23-linux中的定时任务详解
    2020/4/20 一键部署服务
    2020/4/20-ansible一键部署nfs
    2020/4/20-backup之rsync
    2020/4/19-linux中/etc/hosts
    2020/4/18-linux中的selinux
    2020/4/18-linux中的iptables
  • 原文地址:https://www.cnblogs.com/anduinlothar/p/2874615.html
Copyright © 2011-2022 走看看