there are 2 problems with the following code:
1- it gets bandwidth used by all network cards...
2- it gets all bandwidth used....
I wanna be able to change it to do the following:
1- dynamically use the network card I am using to connect to the internet.
2- to show only the bandwidth used by the current application
private void InitializeNetworkInterfaces()
{
PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
InstanceDataCollectionCollection data = performanceCounterCategory.ReadCategory();
interfaces = performanceCounterCategory.GetInstanceNames();
int length = interfaces.Length;
if (length > 0)
{
bandwidthTimer.Enabled = true;
dataSentCounters = new PerformanceCounter[length];
dataReceivedCounters = new PerformanceCounter[length];
}
for (int i = 0; i < length; i++)
{
dataReceivedCounters[i] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", interfaces[i]);
dataSentCounters[i] = new PerformanceCounter("Network Interface", "Bytes Sent/sec", interfaces[i]);
}
}
private void bandwidthTimer_Tick(object sender, EventArgs e)
{
int length = interfaces.Length;
float sendSum = 0;
float receiveSum = 0;
try
{
for (int i = 0; i < length; i++)
{
sendSum += dataSentCounters[i].NextValue();
receiveSum += dataReceivedCounters[i].NextValue();
}
}
catch (Exception) {
bandwidthTimer.Stop();
}
decimal used =(decimal) (sendSum + receiveSum)*8/1000;
bandwidthUsed.Text = (decimal.Round(used, 1)).ToString();
}