InstanceDataCollectionCollection.Item[String] 屬性

定義

為指定的計數器取得執行個體資料。

public:
 property System::Diagnostics::InstanceDataCollection ^ default[System::String ^] { System::Diagnostics::InstanceDataCollection ^ get(System::String ^ counterName); };
public System.Diagnostics.InstanceDataCollection this[string counterName] { get; }
member this.Item(string) : System.Diagnostics.InstanceDataCollection
Default Public ReadOnly Property Item(counterName As String) As InstanceDataCollection

參數

counterName
String

效能計數器的名稱。

屬性值

InstanceDataCollection 項目,InstanceDataCollectionCollection 物件要根據它來建立索引。

例外狀況

counterName 參數為 null

範例

下列程式代碼範例會 PerformanceCounter 接受命令行中的類別名稱和計數器名稱。 它會取得 InstanceDataCollectionCollectionPerformanceCounterCategory。 然後,如果 PerformanceCounter 存在,此範例會使用 Item[] 屬性 (作為索引器參考,) 取得相關聯的 InstanceDataCollection ,並從集合顯示實例名稱。

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

備註

參數 counterName 不區分大小寫。

InstanceDataCollection集合中的每個物件都包含實例之所有計數器的效能數據。 數據會依計數器名稱編製索引,然後依實例名稱編製索引。 索引器會 counterName 使用 參數來逐步執行與此實例數據集合相關聯的計數器。

適用於