CounterCreationDataCollection.Insert Method (Int32, CounterCreationData)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Inserts a CounterCreationData object into the collection, at the specified index.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public void Insert(
	int index,
	CounterCreationData value


Type: System.Int32

The zero-based index of the location at which the CounterCreationData is to be inserted.

Type: System.Diagnostics.CounterCreationData

The CounterCreationData to insert into the collection.

Exception Condition

value is null.


value is not a CounterCreationData object.


index is less than 0.


index is greater than the number of items in the collection.

The following example demonstrates how to use the Insert method to insert a CounterCreationData object into a CounterCreationDataCollection.

string myCategoryName;
int numberOfCounters;
Console.Write("Enter the category Name : ");
myCategoryName = Console.ReadLine();
// Check if the category already exists or not.
if (!PerformanceCounterCategory.Exists(myCategoryName))
    Console.Write("Enter the number of counters : ");
    numberOfCounters = int.Parse(Console.ReadLine());
    CounterCreationData[] myCounterCreationData =
       new CounterCreationData[numberOfCounters];

    for (int i = 0; i < numberOfCounters; i++)
        Console.Write("Enter the counter name for {0} counter ", i);
        myCounterCreationData[i] = new CounterCreationData();
        myCounterCreationData[i].CounterName = Console.ReadLine();
    CounterCreationDataCollection myCounterCollection =
       new CounterCreationDataCollection(myCounterCreationData);
    CounterCreationData myInsertCounterCreationData = new CounterCreationData(
       "CounterInsert", "", PerformanceCounterType.NumberOfItems32);
    // Insert an instance of 'CounterCreationData' in the 'CounterCreationDataCollection'.
    myCounterCollection.Insert(myCounterCollection.Count - 1,
    Console.WriteLine("'{0}' counter is inserted into 'CounterCreationDataCollection'",
    // Create the category.
    PerformanceCounterCategory.Create(myCategoryName, "Sample Category",
    PerformanceCounterCategoryType.SingleInstance, myCounterCollection);

    for (int i = 0; i < numberOfCounters; i++)
        myCounter = new PerformanceCounter(myCategoryName,
           myCounterCreationData[i].CounterName, "", false);
    Console.WriteLine("The index of '{0}' counter is {1}",
       myInsertCounterCreationData.CounterName, myCounterCollection.IndexOf(myInsertCounterCreationData));
    Console.WriteLine("The category already exists");

.NET Framework
Available since 1.1
Return to top