Export (0) Print
Expand All

Retrieving Lists of Counters and Categories

You can use the GetCounters method on the PerformanceCounterCategory class to quickly retrieve a list of all of the counters or instances in a specific category. Each time you request a value from a category by creating an instance of a PerformanceCounter object, setting it to an existing counter, and retrieving a value, the system reads through the entire category and retrieves the requested counter. This means that if you request five different counters from a category that contains 20 counters, the system reads all 20 counters in the category five times, searching for each counter separately. Using GetCounters, you get the same data, but the category is read only once.

The GetCounters method returns an array of type PerformanceCounter that contains the counters in the category. You can use the Item property to work with the contents of the collection.

In addition to retrieving counters, you can also use the static GetCategories method to return a list of categories on the current computer or any server to which you have access. GetCategories returns an array of PerformanceCounterCategories objects.

To retrieve the counters in a category

  1. Create a PerformanceCounter object and configure it so that it points to the desired category. For more information, see Creating PerformanceCounter Component Instances or Configuring PerformanceCounter Component Instances.
  2. Create an array of type PerformanceCounter to contain the resulting category list.
  3. Call the GetCounters method on the PerformanceCounterCategory class and specify the desired category as a parameter.
  4. Set the results to the array.

    The following example shows how to retrieve all of the counters in the Cache category. This code assumes that you are working in a Windows Form that contains a button and a ListBox control. It also assumes that you have a reference to the Systems.Diagnostics namespace and an imports statement (for Visual Basic) or a using statement (for C#) to the same:

    ' Visual Basic
    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
    
    // C#
    private void btnGetCounters_Click(System.Object sender, System.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);
       }
    }
    

To retrieve all categories on a machine

  1. Create an array of type PerformanceCounter to contain the resulting category list.
  2. Call the GetCategories method on the PerformanceCounterCategory class and specify the desired category as a parameter.
  3. Set the results to the array.

    The following code shows how to retrieve all categories on the local machine. This code assumes that you are working in a Windows form that contains a button and a ListBox control. It also assumes that you have a reference to the Systems.Diagnostics namespace and an imports statement (for Visual Basic) or a using statement (for C#) to the same:

    ' Visual Basic
    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
    
    // C#
    private void btnGetCategories_Click(object sender, System.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);
       }
    }
    

See Also

Performance Counter Value Retrieval | Creating PerformanceCounter Component Instances | Configuring PerformanceCounter Component Instances

Show:
© 2014 Microsoft