C# WMI Readout: Win32_PerfRawData_Tcpip_NetworkInterface returns nothing

Viewed 180

I try to read out WMI data with C#. I use the System.Management namespace. For the most WMI-Queries it works quiet good. But when I try to read "BytesTotalPerSec" from Win32_PerfFormattedData_Tcpip_NetworkInterface it doesn't work. My code looks like this:

var select = $"SELECT BytesTotalPerSec FROM 
Win32_PerfFormattedData_Tcpip_NetworkInterface WHERE Name = 'MyAdapter'";
var scope = new ManagementScope($"\\\\{computername}\\root\\cimv2");
scope.Connect();

var query = new ObjectQuery(select);
var searcher = new ManagementObjectSearcher(scope, query);
var queryCollection = searcher.Get();
double value = 0;
foreach (var item in queryCollection)
    value = Convert.ToDouble(item["BytesTotalPerSec"]);

I tried this code with other WMI-Queries and it works. When I debug the code, the queryCollection has no items. Does anyone know what I'm doing wrong?

Sorry for my bad english, I hope you understand my Problem.

1 Answers

I found the solution. The problem isn't because of my code. Windows reads the WMI-Code Win32_PerfFormattedData_Tcpip_NetworkInterface from the performance monitor. All the other WMI-codes I tried read their value with CIM, which is going directly to the OS. Because Win32_PerfFormattedData_Tcpip_NetworkInterface uses the performance monitor, the executing user has to be in the group Performance Monitor Users. When I put my user into this group it works.

Related