Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# get system network usage

Tags:

c#

networking

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;
            }

        }
like image 794
Sandeep Bansal Avatar asked Jan 17 '10 17:01

Sandeep Bansal


1 Answers

You can get the current system's network usage by working with PerformanceCounterCategory too:

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        PerformanceCounterCategory pcg = new PerformanceCounterCategory("Network Interface");
        string instance = pcg.GetInstanceNames()[0];
        PerformanceCounter pcsent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
        PerformanceCounter pcreceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);

        Console.WriteLine("Bytes Sent: {0}", pcsent.NextValue() / 1024);
        Console.WriteLine("Bytes Received: {0}", pcreceived.NextValue() / 1024);
    }
}

Source: http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580

like image 56
ShadowHunter Avatar answered Sep 30 '22 00:09

ShadowHunter