InstanceDataCollectionCollection.Contains(String) Methode

Definition

Bestimmt, ob für den angegebenen Zähler, der durch eines der indizierten InstanceDataCollection-Objekte bezeichnet wird, in der Auflistung eine Auflistung von Instanzdaten vorhanden ist.

public:
 bool Contains(System::String ^ counterName);
public bool Contains (string counterName);
member this.Contains : string -> bool
Public Function Contains (counterName As String) As Boolean

Parameter

counterName
String

Der Name des Leistungsindikators.

Gibt zurück

true, wenn eine Auflistung der Instanzdaten, die den angegebenen Zähler enthält, in der Auflistung vorhanden ist, andernfalls false.

Ausnahmen

Der counterName-Parameter ist null.

Beispiele

Im folgenden Codebeispiel werden ein Kategoriename und ein PerformanceCounter Indikatorname aus der Befehlszeile akzeptiert. Es ruft die InstanceDataCollectionCollection für ab PerformanceCounterCategory und verwendet dann die Contains -Methode, um zu bestimmen, ob der angegebene Indikator vorhanden ist. Wenn der Indikator vorhanden ist, ruft das Beispiel den zugeordneten InstanceDataCollection ab und zeigt die instance Namen aus der Auflistung an.

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

    InstanceDataCollectionCollection idColCol;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        counterName = args[1];
    }
    catch
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // Get the InstanceDataCollectionCollection for this category.
        PerformanceCounterCategory pcc = new PerformanceCounterCategory(categoryName);
        idColCol = pcc.ReadCategory();
    }
    catch(Exception ex)
    {
        Console.WriteLine("An error occurred getting the InstanceDataCollection for " +
            "category \"{0}\"."+ "\n" +ex.Message, categoryName);
        return;
    }

    // Check if this counter name exists using the Contains
    // method of the InstanceDataCollectionCollection.
    if (!idColCol.Contains(counterName))
    {
        Console.WriteLine("Counter \"{0}\" does not exist in category \"{1}\".", counterName, categoryName);
        return;
    }
    else
    {
        // Now get the counter's InstanceDataCollection object using the
        // indexer (Item property) for the InstanceDataCollectionCollection.
        InstanceDataCollection countData = idColCol[counterName];

        ICollection idColKeys = countData.Keys;
        string[] idColKeysArray = new string[idColKeys.Count];
        idColKeys.CopyTo(idColKeysArray, 0);

        Console.WriteLine("Counter \"{0}\" of category \"{1}\" " +
            "has {2} instances.", counterName, categoryName, idColKeys.Count);

        // Display the instance names for this counter.
        int index;
        for(index=0; index<idColKeysArray.Length; index++)
        {
            Console.WriteLine("{0,4} -- {1}", index+1, idColKeysArray[index]);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""

    Dim idColCol As InstanceDataCollectionCollection

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        counterName = args(1)
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Get the InstanceDataCollectionCollection for this category.
        Dim pcc As New PerformanceCounterCategory(categoryName)
        idColCol = pcc.ReadCategory()
    Catch ex As Exception
        Console.WriteLine( _
            "An error occurred getting the InstanceDataCollection for " & _
            "category ""{0}""." & vbCrLf & ex.Message, categoryName)
        Return
    End Try

    ' Check if this counter name exists using the Contains
    ' method of the InstanceDataCollectionCollection.
    If Not idColCol.Contains(counterName) Then
        Console.WriteLine( _
            "Counter ""{0}"" does not exist in category ""{1}"".", _
            counterName, categoryName)
        Return
    Else
        ' Now get the counter's InstanceDataCollection object using the
        ' indexer (Item property) for the InstanceDataCollectionCollection.
        Dim countData As InstanceDataCollection = idColCol(counterName)

        Dim idColKeys As ICollection = countData.Keys
        Dim idColKeysArray(idColKeys.Count - 1) As String
        idColKeys.CopyTo(idColKeysArray, 0)

        Console.WriteLine("Counter ""{0}"" of category ""{1}"" " & _
            "has {2} instances.", counterName, categoryName, idColKeys.Count)

        ' Display the instance names for this counter.
        Dim index As Integer
        For index = 0 To idColKeysArray.Length - 1
            Console.WriteLine("{0,4} -- {1}", index + 1, idColKeysArray(index))
        Next index
    End If
End Sub

Hinweise

Beim counterName Parameter wird die Groß-/Kleinschreibung nicht beachtet.

Jedes InstanceDataCollection Objekt in der Auflistung enthält die Leistungsdaten für alle Leistungsindikatoren für eine instance. Die Daten werden nach dem Zählernamen und dann nach instance Namen indiziert. Contains gibt zurück true , wenn ein InstanceDataCollection Objekt vorhanden ist, dessen zugeordneter Indikator den durch den counterName -Parameter angegebenen Namen aufweist.

Gilt für: