How to: Retrieve 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 PerformanceCounterCategory objects.
To retrieve the counters in a category
Create a PerformanceCounter object and configure it so that it points to the desired category. For more information, see How to: Create PerformanceCounter Component Instances or How to: Configure PerformanceCounter Component Instances.
Create an array of type PerformanceCounter to contain the resulting category list.
Call the GetCounters method on the PerformanceCounterCategory class and specify the desired category as a parameter.
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 Forms application that contains a button and a ListBox control. It also assumes that you have a reference to System.dll and an Imports statement (for Visual Basic) or a using statement (for C#) for the System.Diagnostics namespace: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); } }
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.get_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.get_Items().Add(mypc[i].get_CounterName()); } }
To retrieve all categories on a computer
Create an array of type PerformanceCounter to contain the resulting category list.
Call the GetCategories method on the PerformanceCounterCategory class and specify the desired category as a parameter.
Set the results to the array.
The following code shows how to retrieve all categories on the local computer. This code assumes that you are working in a Windows Forms application that contains a button and a ListBox control. It also assumes that you have a reference to System.dll and an Imports statement (for Visual Basic) or a using statement (for C#) for the System.Diagnostics namespace:
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); } }
private void btnGetCategories_Click(Object sender, System.EventArgs e) { System.Diagnostics.PerformanceCounterCategory myCat2[]; // Clear the list's current contents. this.listBox2.get_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.get_Items().Add(myCat2[i].get_CategoryName()); } }
See Also
Tasks
How to: Create PerformanceCounter Component Instances
How to: Configure PerformanceCounter Component Instances