When you create a new counter, you first create a category, and then specify one or more counters to be placed in it. You can do this in the following way:
Note You can also use the CreateCategory method to create a single counter within a new category. For instructions, see Creating Performance Counter Categories.
It is important to note the difference between creating a counter and creating an instance of the PerformanceCounter component. When you create a counter, you are creating a new category and its associated counters in the Windows operating system, rather than a component in your project or application. When you create an instance of the PerformanceCounter class, you create a component that references an external counter.
To create a new category and set of performance counters programmatically
The following example shows how to create a series of counters as an array and pass it to the category when you create it:
[Visual Basic] dim cdCounter1 As New CounterCreationData dim cdCounter2 As New CounterCreationData dim CounterDatas(2) as New CounterCreationData cdCounter1.CounterName = strCountName cdCounter1.CounterHelp = strCountName & " help string" cdCounter1.CounterType = NumberOfItems64 assign CounterDatas(0) = cdCounter1 cdCounter2.CounterName = strCountName cdCounter2.CounterHelp = strCountName & " help string" cdCounter2.CounterType = NumberOfItems64 assign CounterDatas(1) = cdCounter2 dim pcTmp as PerformanceCounter pcTmp.CreateCategory "Multi Counter Category", "Category help", CounterDatas [C#] CounterCreationData cdCounter1 = new CounterCreationData(); CounterCreationData cdCounter2 = new CounterCreationData(); CounterCreationData[] CounterDatas = new CounterCreationData[2]; cdCounter1.CounterName = strCountName; cdCounter1.CounterHelp = strCountName + " help string"; cdCounter1.CounterType = NumberOfItems64; CounterDatas[0] = cdCounter1; cdCounter2.CounterName = strCountName; cdCounter2.CounterHelp = strCountName + " help string"; cdCounter2.CounterType = NumberOfItems64; CounterDatas(1) = cdCounter2; PerformanceCounter pcTmp = new PerformanceCounter(); pcTmp.CreateCategory ("Multi Counter Category", "Category help", CounterDatas);
Creating Performance Counter Categories | Category and Counter Management