tags:

views:

250

answers:

1

I need a way to get the current system network usage, up and down.

I found some on the net but they're not working out for me.

Thanks for your help

Code snippet:


 private void timerPerf_Tick(object sender, EventArgs e)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
                return;

            NetworkInterface[] interfaces
                = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface ni in interfaces)
            {
                this.labelnetup.Text = "    Bytes Sent: " + ni.GetIPv4Statistics().BytesSent;
                this.labelnetdown.Text = "    Bytes Rec: " + ni.GetIPv4Statistics().BytesReceived;
            }

        }
+4  A: 

Please see Report information from NetworkInterface: network statistics for a good sample program:

using System;
using System.Net.NetworkInformation;

class MainClass
{
    static void Main()
    {
        if (!NetworkInterface.GetIsNetworkAvailable())
           return;

        NetworkInterface[] interfaces 
            = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface ni in interfaces)
        {                
            Console.WriteLine("    Bytes Sent: {0}", 
                ni.GetIPv4Statistics().BytesSent);
            Console.WriteLine("    Bytes Received: {0}",
                ni.GetIPv4Statistics().BytesReceived);
        }
    }
}
Andrew Hare
Thanks for the snippet, how can this be done inside a Windows Form, with a timer.I have posted a snippet with your code in my question
Sandeep Bansal
Your code snippet looks fine - is it not working?
Andrew Hare
No it's returning 0 all the time, and I try to set a download but no changes in the numbers.
Sandeep Bansal