Reputation:
I want to get the number of consumed MBs on the internet by code..
I know that it can be done manually: Settings -> Network & Internet -> Data Usage (Windows 10)
But how can I find this by code?
I want the number for the whole system, NOT ONLY MY APPLICATION.
For example, I want my code to show: The GB used this month was 3.21!
Upvotes: 3
Views: 598
Reputation: 651
You can try the following snippet which will give you total sent and received data. You just need to sum it out:
private static void GetTrafficStatistics()
{
PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
string instance = performanceCounterCategory.GetInstanceNames()[0]; // 1st NIC !
PerformanceCounter performanceCounterSent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
PerformanceCounter performanceCounterReceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("bytes sent: {0}k\tbytes received: {1}k", performanceCounterSent.NextValue() / 1024, performanceCounterReceived.NextValue() / 1024);
Thread.Sleep(500);
}
}
PS:It is using System.Diagnostics.dll
Hope it will help you out.
Upvotes: 1