Click here to Skip to main content
15,302,239 members
Articles / Programming Languages / C# 4.0
Posted 11 Feb 2010


29 bookmarked

Performance Counters Enumerator

Rate me:
Please Sign up or sign in to vote.
4.50/5 (9 votes)
11 Feb 2010CPOL2 min read
Enumerate all performance counters registered on a machine and save the result in an XML file


Some articles have already been written about Windows Performance Counters. Nevertheless, I still experience that their potential is underestimated by administrators and often almost unknown to many software developers.


One of the main reasons for this is the fact that they are just too many Performance counters! On a plain end-user machine, they are several hundreds of them. The amount of performance counters increases vertically on a Server.


The goal of this article is to present a way to enumerate all performance counters registered on a local machine and to give the possibility to save the result of the enumeration in an XML file. Once saved in an XML file, one can transform the result using XSLT and present it in a nice «look and feel» report.


The motivation is to make a kind of snapshot of all performance counters available on a machine. This will help developers and administrators to choose the ideal performance counters based on their description when needed. Often when dealing with diagnose and the Windows built-in «Performance Monitor», I am overwhelmed with the huge amount of these counters and the lack of having a comprehensive list of the performance counters without having to click several hundred times in the MMC dialog.


Using the available .NET Diagnostic classes, it is just a matter of a few minutes to reach this goal.

Since this project only enumerates the category of performance counters and all performance counters belonging to these categories, I only need two of the available classes that deal with performance counters.

PerformanceCounterCategory and PerformanceCounter both belong to the System.Diagnostics Namespace which, as stated in the MSDN documentation, mainly provides classes that allow you to interact with:

  • system processes
  • event logs
  • and performance counters

Having said that, this tool consists of four steps:

  1. Retrieve the name of the local machine where the tool runs:
    // Retrieve the machine's name. 
    txtMachine.Text = Environment.MachineName; 
  2. Retrieve the Categories of Performance Counters available on the machine:
    // Get the Categories of Performance Counters
    PerformanceCounterCategory[] categories = performanceCountersMgr.Categories;
    for (int i = 0; i < categories.Length; i++)
        // Show these at the UI
  3. For any selected Category, retrieve the Counters available:
    // Collect Counters for the selected Category
    ListView.SelectedListViewItemCollection sel = listViewCategories.SelectedItems;
    if(sel != null && sel.Count > 0)
        // Show Description of the selected Category
        PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
        textBoxCategoryDescription.Text = category[sel[0].Index].CategoryHelp;
        // Acquire the Counters of the selected Category
        PerformanceCounter[] counters = 
        if (counters != null)
            for (int i = 0; i < counters.Length; i++)
                PerformanceCounter counter = counters[i];
            listViewCounters.Items[0].Selected = true;
  4. Save the result at the desired location as an XML File:
    XmlTextWriter xml = new XmlTextWriter(filename, Encoding.UTF8);
    PerformanceCounterCategory[] category = performanceCountersMgr.Categories;
    for (int i = 0; i < category.Length; i++)
        if (checkBoxCategoryDescription.Checked)
    // End of Categories Element


This article shows how to enumerate the performance counters on a local machine using just a few of the available .NET Diagnostic classes and how to serialize the enumeration in an XML file using the XmlTextWriter class. The enumeration of these on a remote machine is left as an exercise for the reader.


  • 11.02.2010 - Enumeration of the categories of performance counters and their associated counters


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

marc ochsenmeier
Software Developer winitor
Germany Germany
Marc Ochsenmeier is the author of pestudio ( and worked as developer with the focus on Windows Security. He now works as a Malware Analyst

pestudio is on twitter at:

Comments and Discussions

GeneralThis is great! Pin
CZabransky10-Mar-11 6:52
MemberCZabransky10-Mar-11 6:52 
I'm working on a performance monitoring client and this information is very useful!

My concern is that I need to be able to iterate through each instance contained within a counter. Do you know of a way to do this?
GeneralRe: This is great! Pin
marc ochsenmeier16-May-11 6:14
Membermarc ochsenmeier16-May-11 6:14 
GeneralMy vote of 5 Pin
Dirk Held4-Nov-10 3:19
MemberDirk Held4-Nov-10 3:19 
GeneralRe: My vote of 5 Pin
marc ochsenmeier16-May-11 6:12
Membermarc ochsenmeier16-May-11 6:12 
GeneralMinor bug and question Pin
klanglieferant15-Feb-10 20:03
Memberklanglieferant15-Feb-10 20:03 
GeneralRe: Minor bug and question Pin
marc ochsenmeier21-Feb-10 22:43
Membermarc ochsenmeier21-Feb-10 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.