Finding Network statistics in Dot Net
Last few days while working with my broadband network
(downloading few stuff) I wanted to check the exact amount
of download per minute. I tried to check the data from task
manager and resource monitor. But these gave data in terms
of percentage of network use. Hence I could never find the
exact amount of download.
So I wrote a small application to sniff the network and find
out the exact amount of download. After fetching the data we
can do lots of thing like creating speed chart etc…
To fetch the network we use the
System.Net.NetworkInformation namespace
First we need to get the network Interface to track. A Machine might be connected to many network and we need to track each network differently. We can do that by the code below.
foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (currentNetworkInterface.Name.ToLower() == "NameOf
network")
{
networkInterface = currentNetworkInterface;
break;
}
}
We get the network usage of the current network with the help of GetIPv4Statistics function which returns object of the IPv4InterfaceStatistics class.
IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();
You can find the total bytes send and received with the help
of the property.
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived
To find the speed of the download and upload you need to find the difference of the Bytes send and received between required time interval and you will have the speed.
Remember bytes send and received are kept differently hence total download = bytes send + bytes received.
Vikram