InstanceDataCollection.Item Property (String)


Gets the instance data associated with this counter. This is typically a set of raw counter values.

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

public InstanceData this[
	string instanceName
] { get; }


Type: System.String

The name of the performance counter category instance, or an empty string ("") if the category contains a single instance.

Property Value

Type: System.Diagnostics.InstanceData

An InstanceData item, by which the InstanceDataCollection object is indexed.

Exception Condition

The instanceName parameter is null.

The instanceName parameter is case-insensitive.

The following code example accepts a PerformanceCounter category name, counter name, and instance name from the command line. It gets the InstanceDataCollectionCollection for the category, which is a collection of InstanceDataCollection objects. From that it gets the particular InstanceDataCollection for the specified counter. Then, if the instance exists, the example uses the Item property (referenced as an indexer) to obtain the associated InstanceData object.

public static void Main(string[] args)
    // These values can be used as arguments.
    string categoryName = "Process";
    string counterName = "Private Bytes";
    string instanceName = "Explorer";

    InstanceDataCollection idCol;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

    // Copy the supplied arguments into the local variables.
        categoryName = args[0];
        counterName = args[1];
        instanceName = args[2];
        // Ignore the exception from non-supplied arguments.

        // Get the InstanceDataCollectionCollection for this category.
        PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName);
        InstanceDataCollectionCollection idColCol = pcc.ReadCategory();

        // Get the InstanceDataCollection for this counter.
        idCol = idColCol[counterName];
        if (idCol==null)
            throw new Exception("Counter does not exist.");
    catch(Exception ex)
        Console.WriteLine("An error occurred getting the InstanceDataCollection for " +
            "category \"{0}\", counter \"{1}\"." +  "\n" + ex.Message, categoryName, counterName);

    // If the instance name is empty, use the single-instance name.
    if (instanceName.Length==0)
        instanceName = SINGLE_INSTANCE_NAME;

    // Check if this instance name exists using the Contains
    // method of the InstanceDataCollection.
    if (!idCol.Contains(instanceName))
        Console.WriteLine("Instance \"{0}\" does not exist in counter \"{1}\", " +
            "category \"{2}\".", instanceName, counterName, categoryName);
        // The instance name exists, now get its InstanceData object
        // using the indexer (Item property) for the InstanceDataCollection.
        InstanceData instData = idCol[instanceName];

        Console.WriteLine("CategoryName: {0}", categoryName);
        Console.WriteLine("CounterName:  {0}", counterName);
        Console.WriteLine("InstanceName: {0}", instData.InstanceName);
        Console.WriteLine("RawValue:     {0}", instData.RawValue);

.NET Framework
Available since 1.1
Return to top