This documentation is archived and is not being maintained.

CounterCreationDataCollection.Insert Method

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.


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

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.