更新:2007 年 11 月
可以使用 PerformanceCounterCategory 类的 GetCounters 方法,快速检索特定类别中所有计数器或实例的列表。每当您创建 PerformanceCounter 对象的实例,将它设置为现有的计数器并检索值,从而从类别中请求一个值时,系统就会读取整个类别并检索所请求的计数器。也就是说,如果从包含 20 个计数器的类别中请求五个不同的计数器,系统就把类别中所有这 20 个计数器读五遍,分别搜索各个计数器。通过使用 GetCounters,您将获得同样的数据,但仅读取一次类别。
GetCounters 方法返回一个 PerformanceCounter 类型的数组,其中包含该类别中的计数器。可以使用 Item 属性来处理该集合的内容。
除了检索计数器外,还可以使用静态的 GetCategories 方法来返回当前计算机或任何可访问的服务器上的类别列表。GetCategories 返回 PerformanceCounterCategory 对象数组。
检索类别中的计数器
创建一个 PerformanceCounter 对象并对它进行配置,使它指向所需的类别。有关更多信息,请参见如何:创建 PerformanceCounter 组件实例或如何:配置 PerformanceCounter 组件实例。
创建一个 PerformanceCounter 类型的数组,以包含所产生的类别列表。
调用 PerformanceCounterCategory 类的 GetCounters 方法,并将所需的类别指定为一个参数。
将结果设置到数组中。
下面的示例演示如何检索 Cache 类别中的所有计数器。这段代码假设您使用一个包含一个按钮和一个 ListBox 控件的 Windows 窗体应用程序。此外,还假设您引用了 System.dll,并对 System.Diagnostics 命名空间使用了一个 Imports 语句(对于 Visual Basic)或一个 using 语句(对于 C#):
Private Sub btnGetCounters_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGetCounters.Click Dim mypc() As PerformanceCounter Dim i As Integer Dim myCat As New PerformanceCounterCategory("Cache") ' Remove the current contents of the list. Me.ListBox1.Items.Clear() ' Retrieve the counters. mypc = myCat.GetCounters ' Add the retrieved counters to the list. For i = 0 To mypc.Length - 1 Me.ListBox1.Items.Add(mypc(i).CounterName) Next End Sub
private void btnGetCounters_Click(object sender, EventArgs e) { System.Diagnostics.PerformanceCounter[] mypc; System.Diagnostics.PerformanceCounterCategory mycat = new System.Diagnostics.PerformanceCounterCategory("cache"); // Remove the current contents of the list. this.listBox1.Items.Clear(); // Retrieve the counters. mypc = mycat.GetCounters(); // Add the retrieved counters to the list. for (int i = 0; i < mypc.Length; i++) { this.listBox1.Items.Add(mypc[i].CounterName); } }
检索计算机上的所有类别
创建一个 PerformanceCounter 类型的数组,以包含所产生的类别列表。
调用 PerformanceCounterCategory 类的 GetCategories 方法,并将所需的类别指定为一个参数。
将结果设置到数组中。
下面的代码演示如何检索本地计算机上的所有类别。这段代码假设您使用一个包含一个按钮和一个 ListBox 控件的 Windows 窗体应用程序。此外,还假设您引用了 System.dll,并对 System.Diagnostics 命名空间使用了一个 Imports 语句(对于 Visual Basic)或一个 using 语句(对于 C#):
Private Sub btnGetCategories_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim myCat2 As PerformanceCounterCategory() Dim i As Integer ' Clear the contents of the list box. Me.listBox2.Items.Clear() ' Retrieve the categories. myCat2 = PerformanceCounterCategory.GetCategories ' Add the retrieved categories to the list. For i = 0 To myCat2.Length - 1 Me.listBox2.Items.Add(myCat2(i).CategoryName) Next End Sub
private void btnGetCategories_Click(object sender, EventArgs e) { System.Diagnostics.PerformanceCounterCategory[] myCat2; // Clear the list's current contents. this.listBox2.Items.Clear(); // Retrieve the categories. myCat2 = System.Diagnostics.PerformanceCounterCategory.GetCategories(); // Add the retrieved categories to the list. for (int i = 0; i < myCat2.Length; i++) { this.listBox2.Items.Add(myCat2[i].CategoryName); } }