2010-02-05 6 views
4

Pour mon application, je veux montrer à l'utilisateur la bande passante du réseau. Ainsi, le délai de téléchargement sera connu de l'utilisateur. Est-il possible de leur montrer?Mesure Bandwidh réseau en C#

+0

@ RomanoZumbé cette question a plus de 7 ans. à ce moment-là, les gens ne chercheront pas une mvve à upvote. c'est la phase initiale. S'il vous plaît ne pas abuser ceux qui contribuent de la phase de démarrage du site –

+1

@SagarV Je ne suis pas "abuser" personne. Cette question a surgi dans ma file d'attente "Close Votes". Je pense que si quelque chose ne répond pas à nos critères de qualité aujourd'hui, il devrait être supprimé même s'il répondait aux critères de qualité lors de sa publication. Mais je comprends maintenant pourquoi il a été voté en premier lieu. –

Répondre

2

Essayez celui-ci: How to calculate network bandwidth speed in c#.

enter image description here

using System; 
using System.Net.NetworkInformation; 
using System.Windows.Forms; 

namespace InterfaceTrafficWatch 
{ 
    /// <summary> 
    /// Network Interface Traffic Watch 
    /// by Mohamed Mansour 
    /// 
    /// Free to use under GPL open source license! 
    /// </summary> 
    public partial class MainForm : Form 
    { 
     /// <summary> 
     /// Timer Update (every 1 sec) 
     /// </summary> 
     private const double timerUpdate = 1000; 

     /// <summary> 
     /// Interface Storage 
     /// </summary> 
     private NetworkInterface[] nicArr; 

     /// <summary> 
     /// Main Timer Object 
     /// (we could use something more efficient such 
     /// as interop calls to HighPerformanceTimers) 
     /// </summary> 
     private Timer timer; 

     /// <summary> 
     /// Constructor 
     /// </summary> 
     public MainForm() 
     { 
      InitializeComponent(); 
      InitializeNetworkInterface(); 
      InitializeTimer(); 
     } 

     /// <summary> 
     /// Initialize all network interfaces on this computer 
     /// </summary> 
     private void InitializeNetworkInterface() 
     { 
      // Grab all local interfaces to this computer 
      nicArr = NetworkInterface.GetAllNetworkInterfaces(); 

      // Add each interface name to the combo box 
      for (int i = 0; i < nicArr.Length; i++) 
       cmbInterface.Items.Add(nicArr[i].Name); 

      // Change the initial selection to the first interface 
      cmbInterface.SelectedIndex = 0; 
     } 

     /// <summary> 
     /// Initialize the Timer 
     /// </summary> 
     private void InitializeTimer() 
     { 
      timer = new Timer(); 
      timer.Interval = (int)timerUpdate; 
      timer.Tick += new EventHandler(timer_Tick); 
      timer.Start(); 
     } 

     /// <summary> 
     /// Update GUI components for the network interfaces 
     /// </summary> 
     private void UpdateNetworkInterface() 
     { 
      // Grab NetworkInterface object that describes the current interface 
      NetworkInterface nic = nicArr[cmbInterface.SelectedIndex]; 

      // Grab the stats for that interface 
      IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); 

      // Calculate the speed of bytes going in and out 
      // NOTE: we could use something faster and more reliable than Windows Forms Tiemr 
      //  such as HighPerformanceTimer http://www.m0interactive.com/archives/2006/12/21/high_resolution_timer_in_net_2_0.html 
      int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text))/1024; 
      int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text))/1024; 

      // Update the labels 
      lblSpeed.Text = nic.Speed.ToString(); 
      lblInterfaceType.Text = nic.NetworkInterfaceType.ToString(); 
      lblSpeed.Text = nic.Speed.ToString(); 
      lblBytesReceived.Text = interfaceStats.BytesReceived.ToString(); 
      lblBytesSent.Text = interfaceStats.BytesSent.ToString(); 
      lblUpload.Text = bytesSentSpeed.ToString() + " KB/s"; 
      lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s"; 

     } 

     /// <summary> 
     /// The Timer event for each Tick (second) to update the UI 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     void timer_Tick(object sender, EventArgs e) 
     { 
      UpdateNetworkInterface(); 
     } 

    } 
} 
+0

alors que c'est correct, cela montrera toute la bande passante de l'adaptateur réseau, et pas pour un téléchargement spécifique –

0

Si ce que vous téléchargez via un flux alors ce que vous pouvez faire est de faire une sorte de « mètre », qui hérite de cours d'eau et expose une propriété qui est une somme de les octets lus ou écrits dans son objet de flux sous-jacent. Avoir le compteur encapsuler l'objet de flux que vous utilisez pour télécharger, puis lire à partir de l'objet compteur. alors, si souvent, vous pouvez vérifier le nombre d'octets lus au cours du temps pour calculer la vitesse de bande passante consommée.