InstanceDataCollection.Contains Method (String)


Determines whether a performance instance with a specified name (identified by one of the indexed InstanceData objects) exists in the collection.

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

public bool Contains(
	string instanceName


Type: System.String

The name of the instance to find in this collection.

Return Value

Type: System.Boolean

true if the instance exists in the collection; otherwise, false.

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. It then uses the Contains method to determine if the specified instance exists, using the default single-instance name if none is entered.

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