InstanceDataCollection.Contains(String) Metoda

Definicja

Określa, czy wystąpienie wydajności o określonej nazwie (identyfikowane przez jeden z indeksowanych InstanceData obiektów) istnieje w kolekcji.

C#
public bool Contains (string instanceName);

Parametry

instanceName
String

Nazwa wystąpienia do znalezienia w tej kolekcji.

Zwraca

true jeśli wystąpienie istnieje w kolekcji; w przeciwnym razie , false.

Wyjątki

Parametr instanceName ma wartość null.

Przykłady

Poniższy przykład kodu akceptuje PerformanceCounter nazwę kategorii, nazwę licznika i nazwę wystąpienia z wiersza polecenia. Pobiera wartość InstanceDataCollectionCollection dla kategorii, która jest kolekcją InstanceDataCollection obiektów. Z tego pobiera dane InstanceDataCollection dla określonego licznika. Następnie używa Contains metody , aby określić, czy określone wystąpienie istnieje, przy użyciu domyślnej nazwy pojedynczego wystąpienia, jeśli nie zostanie wprowadzona żadna.

C#
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.
    try
    {
        categoryName = args[0];
        counterName = args[1];
        instanceName = args[2];
    }
    catch
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // 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);
        return;
    }

    // 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);
        return;
    }
    else
    {
        // 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);
    }
}

Uwagi

Parametr instanceName nie uwzględnia wielkości liter.

Dotyczy

Produkt Wersje
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9