My brother was asking for help on how to get the information from network interface so that it can be use to monitor the network activity. I did some googling and I've found this piece of code. Simple enough but can be very useful if we expand it further.
private static void GetNetworkInfo()
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
if (NetworkInterface.GetIsNetworkAvailable())
{
foreach (NetworkInterface iface in interfaces)
{
if (iface.OperationalStatus == OperationalStatus.Up && iface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
IPv4InterfaceStatistics stats = iface.GetIPv4Statistics();
long newsentbytes = stats.BytesSent;
long newreceivedbytes = stats.BytesReceived;
long sentbytes = newsentbytes - (oldsentbytes == 0 ? newsentbytes : oldsentbytes);
long receivedbytes = newreceivedbytes - (oldreceivedbytes == 0 ? newreceivedbytes : oldreceivedbytes);
Console.WriteLine("Bytes sent: {0}", sentbytes.ToString());
Console.WriteLine("Bytes received: {0}", receivedbytes.ToString());
oldsentbytes = newsentbytes;
oldreceivedbytes = newreceivedbytes;
}
}
}
}
No comments:
Post a Comment