用于网络接口性能计数器的C#实例名称

时间:2015-03-12 17:46:33

标签: c# wpf performancecounter

我正在尝试将一些系统监视功能添加到我正在处理的WPF应用程序中,并且我想添加一个带宽使用监视器。现在我有一个CPU和一个RAM计数器工作,但我无法弄清楚什么用于性能计数器实例名称(抱歉,如果这不是正确的术语)。这就是我到目前为止所使用的:

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", );

我有一个字符串,使用计时器刻度方法每1秒更新一次,更新我的WPF标签,RAM和CPU计数器工作,但我不知道为最后两个网络接口的实例名称放置什么。 / p>

提前致谢。

1 个答案:

答案 0 :(得分:6)

以下是一些如何迭代网络接口的示例代码;

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

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

通过使用 PerformanceCounterCategory ,您现在可以获取实例名称,然后使用 GetInstanceNames 在PerformanceCounter中使用。