mk06818
mk06818

Reputation: 1

Get network information with WMI

I have to create a resource monitoring dashboard (like Task Manager) in my web project. I'm using WMI. I could get CurrentBandwidth and BytesTotalPersec from Win32_PerfFormattedData_Tcpip_NetworkInterface but it returns 7 items (Task Manager shows just 4 items in the Network tab). I found that some of the items are disabled; I can find which ones are enabled using NetEnabled in Win32_NetworkAdapter, but how can I join these two? How can I show network information just like Task Manager?

I want to get network information like Task Manager shows (e.g. AdapterName, Utilization (for this I have to get bandwidth and total send/receive bytes/sec), Speed, and state). I can get some of this by using Win32_PerfFormattedData_Tcpip_NetworkInterface and others by using Win32_NetworkAdapter. For example, I can get all networks' send/receive bytes/sec with BytesTotalPersec member of 'Win32_PerfFormattedData_Tcpip_NetworkInterface', but this retrieves all networks' (including disabled) data. How can I get the send/receive bytes/sec measurement for only enabled networks?

Upvotes: 0

Views: 2837

Answers (1)

Silas Reinagel
Silas Reinagel

Reputation: 4203

The best way to do this is to use WQL Queries.

http://msdn.microsoft.com/en-us/library/aa392902(v=vs.85).aspx

So your first query will be something like this

"SELECT Name FROM Win32_NetworkAdapter WHERE NetEnabled = 'true'"

After you have that list, you can do a for-each on that IEnumerable and call a query like like on each one:

"SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface WHERE Name = '" + adapter + "'"

But, you might find some interesting problems like the fact that the names don't always match exactly. On my machine, the NetworkAdapterWMI prefixes the name with "Intel(R)" and the NetworkInterfacesWMI prefixes the same name with "Intel[R]" so you might need to do a little transformation between the two queries.

Hopefully this will get you start in the right direction.

Upvotes: 1

Related Questions