C# Instance Name to Use For Network Interface Performance Counter

眉间皱痕 提交于 2019-12-11 03:15:13

问题


I'm trying to add some system monitoring functions to a WPF app I am working on and I would like to add a bandwidth use monitor. Right now I have a CPU and a RAM counter working but I can't figure out what to use for the Performance Counter instance name (sorry if that isn't the correct term). This is what I'm using so far:

PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", string.Empty);                        
PerformanceCounter netSentCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", );
PerformanceCounter netRecCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", );

I have strings that update every 1 second using a timer tick method that updates my WPF labels and the RAM and the CPU counters work but I don't know what to put for the instance names for the last two network interfaces.

Thanks in advance.


回答1:


Here is some sample code how to iterate through the Network Interfaces;

    PerformanceCounterCategory category = new PerformanceCounterCategory("Network Interface");
    String[] instancename = category.GetInstanceNames();

    foreach (string name in instancename)
    {
        Console.WriteLine(name);
    }

By utilizing the PerformanceCounterCategory, you can now obtain the Instance Names, and then use in your PerformanceCounter(s) using GetInstanceNames.



来源:https://stackoverflow.com/questions/29016769/c-sharp-instance-name-to-use-for-network-interface-performance-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!