user4860969
user4860969

Reputation:

How do i get the information of Network Utilization , Link speed and state of Adapter as shown in the Task manager's Networking tab?

I need to show the same information in my win form program as shown in the task manager. enter image description here

I want to show the same information for remote computer also. If i have an ip of any other system which is connected to my system then i can also get the same information of those system.

Upvotes: 3

Views: 2441

Answers (1)

Artem Kulikov
Artem Kulikov

Reputation: 2296

For getting simple information about network adapters you should use NetworkInterface. It provides configuration and statistical information for a network interface.

public void EchoNetworkInformation()
{
   System.Net.NetworkInformation.NetworkInterface[] nics;
   nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
       foreach (var nic in nics)
        {
            if (nic.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
                continue;

           Console.WriteLine("Adapter name: " + nic.Name);
           Console.WriteLine("Link speed: " + nic.Speed);
           Console.WriteLine("State: " + nic.OperationalStatus);

           var utilization = GetNetworkUtilization(nic.Description);
           Console.WriteLine("Network utilization", utilization);

        }
}

About network utilization - it's more complicated. It uses the following formula for the network utilization:

%utilization = ((8*(dataSent + dataReceived))/bandwidth*time_in_sec)) * 100

I found great article about calculation. You can find here some useful information.
To reduce your time - you can use this code, which resolve this task. It use PerformanceCounter which provides performance information.

/// <summary> Calculate network utilization </summary>
/// <param name="networkCard">Description of network card from NetworkInterface</param>
/// <returns>NetworkUtilization</returns>
public static double GetNetworkUtilization(string networkCard)
{
   const int numberOfIterations = 10;

   // Condition networkCard;
   networkCard = networkCard.Replace("\\", "_");
   networkCard = networkCard.Replace("/", "_");
   networkCard = networkCard.Replace("(", "[");
   networkCard = networkCard.Replace(")", "]");
   networkCard = networkCard.Replace("#", "_");

   var bandwidthCounter = new PerformanceCounter("Network Interface", "Current Bandwidth", networkCard);

   var bandwidth = bandwidthCounter.NextValue();

   var dataSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);
   var dataReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", networkCard);

   float sendSum = 0;
   float receiveSum = 0;

   for (var index = 0; index < numberOfIterations; index++)
   {
      sendSum += dataSentCounter.NextValue();
      receiveSum += dataReceivedCounter.NextValue();
   }

   var dataSent = sendSum;
   var dataReceived = receiveSum;

   double utilization = (8 * (dataSent + dataReceived)) / (bandwidth * numberOfIterations) * 100;

   return utilization;
}

Upvotes: 2

Related Questions