Network Monitor in C#

29 Jun

Ever thought about checking the internet uploading/downloading speed via C#? Well this network monitor can show you how!

Include using System.Net.NetworkInformation; in the .cs file
Drag 5 Labels onto the form.
Drag a timer and change the Enabled property to True and the Interval to 1000.
Intialize the following global variables:

NetworkInterface networkInterface;
long lngBytesSend = 0;
long lngBtyesReceived = 0;

Write the following code in the constructor:

foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
if (currentNetworkInterface.Name.ToLower() == “local area connection”)
networkInterface = currentNetworkInterface;

In the timer’s tick event call the function for showing the network traffic “ShowNetworkInterfaceTraffice()“.

Write the following code in the ShowNetworkInterfaceTraffice() function:

private void ShowNetworkInterfaceTraffice()
IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();
double bytesSentSpeed = (double)(interfaceStatistic.BytesSent – lngBytesSend) / 1024;
double bytesReceivedSpeed = (double)(interfaceStatistic.BytesReceived – lngBtyesReceived) / 1024;
labelSpeed.Text = (networkInterface.Speed / 1000000).ToString() + ” Mbps”;
labelReceived.Text = interfaceStatistic.UnicastPacketsReceived.ToString();
labelSent.Text = interfaceStatistic.UnicastPacketsSent.ToString();
labelUploading.Text = bytesSentSpeed.ToString() + ” KB/s”;
labelDownloading.Text = bytesReceivedSpeed.ToString() + ” KB/s “;
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived;

You can download the project from the Box on the right!

Leave a comment

Posted by on June 29, 2011 in C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: