我希望通过代码获取互联网上消费的MB数量。
我知道可以手动完成: 设置 - >网络&互联网 - >数据使用(Windows 10)
但是如何通过代码找到它呢?
我想要整个系统的数字,而不仅仅是我的应用程序。
例如,我希望我的代码显示:本月使用的GB是3.21!
答案 0 :(得分:1)
您可以尝试以下代码段,它会为您提供总发送和接收的数据。你只需要总结出来:
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:正在使用System.Diagnostics.dll
希望它会帮助你。