PerformanceCounterCategory.GetCounters Metodo

Definizione

Recupera un elenco di contatori in questa categoria di contatori delle prestazioni.

Overload

GetCounters()

Recupera un elenco di contatori in una categoria di contatori delle prestazioni che contiene esattamente un'istanza.

GetCounters(String)

Recupera un elenco di contatori in una categoria di contatori delle prestazioni che contiene una o più istanze.

GetCounters()

Recupera un elenco di contatori in una categoria di contatori delle prestazioni che contiene esattamente un'istanza.

public:
 cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters();
public System.Diagnostics.PerformanceCounter[] GetCounters ();
member this.GetCounters : unit -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters () As PerformanceCounter()

Restituisce

Una matrice di oggetti PerformanceCounter che indica i contatori associati a questa categoria di contatori delle prestazioni a una istanza.

Eccezioni

La categoria non è una singola istanza.

Una chiamata ad un'API di sistema sottostante non ha avuto esito positivo.

Alla categoria non è associata un'istanza.

Codice eseguito senza privilegi di amministratore ha tentato di leggere un contatore delle prestazioni.

Esempio

Nell'esempio PerformanceCounter di codice seguente viene visualizzato un elenco degli oggetti in un PerformanceCounterCategoryoggetto . Crea prima di tutto un oggetto PerformanceCounterCategory con il costruttore appropriato, in base al fatto che sia stato specificato un nome computer. Usa quindi il metodo per restituire una matrice di PerformanceCounter oggetti, selezionando l'overload GetCounters in base al fatto che sia stato specificato un nome dell'istanzaGetCounters.

Questo GetCounters() overload non riesce a meno che non venga usato con una categoria a istanza singola.

public:
    static void Main(array<String^>^ args)
    {
        String^ categoryName = "";
        String^ machineName = "";
        String^ instanceName = "";
        PerformanceCounterCategory^ pcc;
        array<PerformanceCounter^>^ counters;

        // Copy the supplied arguments into the local variables.
        try
        {
            categoryName = args[1];
            machineName = args[2]=="."? "": args[2];
            instanceName = args[3];
        }
        catch (...)
        {
            // Ignore the exception from non-supplied arguments.
        }

        try
        {
            // Create the appropriate PerformanceCounterCategory object.
            if (machineName->Length>0)
            {
                pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
            }
            else
            {
                pcc = gcnew PerformanceCounterCategory(categoryName);
            }

            // Get the counters for this instance or a single instance
            // of the selected category.
            if (instanceName->Length > 0)
            {
                counters = pcc->GetCounters(instanceName);
            }
            else
            {
                counters = pcc->GetCounters();
            }

        }
        catch (Exception^ ex)
        {
            Console::WriteLine("Unable to get counter information for " +
                (instanceName->Length>0? "instance \"{2}\" in ": "single-instance ") +
                "category \"{0}\" on " + (machineName->Length>0? "computer \"{1}\":": "this computer:"),
                categoryName, machineName, instanceName);
            Console::WriteLine(ex->Message);
            return;
        }

        // Display the counter names if GetCounters was successful.
        if (counters != nullptr)
        {
            Console::WriteLine("These counters exist in " +
                (instanceName->Length > 0 ? "instance \"{1}\" of" : "single instance") +
                " category {0} on " + (machineName->Length > 0 ? "computer \"{2}\":": "this computer:"),
                categoryName, instanceName, machineName);

            // Display a numbered list of the counter names.
            int objX;
            for(objX = 0; objX < counters->Length; objX++)
            {
                Console::WriteLine("{0,4} - {1}", objX + 1, counters[objX]->CounterName);
            }
        }
    }
public static void Main(string[] args)
{
    string categoryName = "";
    string machineName = "";
    string instanceName = "";
    PerformanceCounterCategory pcc;
    PerformanceCounter[] counters;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        machineName = args[1]=="."? "": args[1];
        instanceName = args[2];
    }
    catch
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // Create the appropriate PerformanceCounterCategory object.
        if (machineName.Length>0)
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }

        // Get the counters for this instance or a single instance
        // of the selected category.
        if (instanceName.Length>0)
        {
            counters = pcc.GetCounters(instanceName);
        }
        else
        {
            counters = pcc.GetCounters();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get counter information for " +
            (instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
            "category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
            categoryName, machineName, instanceName);
        Console.WriteLine(ex.Message);
        return;
    }

    // Display the counter names if GetCounters was successful.
    if (counters!=null)
    {
        Console.WriteLine("These counters exist in " +
            (instanceName.Length>0? "instance \"{1}\" of": "single instance") +
            " category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
            categoryName, instanceName, machineName);

        // Display a numbered list of the counter names.
        int objX;
        for(objX=0; objX<counters.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim instanceName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim counters() As PerformanceCounter

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        machineName = IIf(args(1) = ".", "", args(1))
        instanceName = args(2)
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Create the appropriate PerformanceCounterCategory object.
        If machineName.Length > 0 Then
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        Else
            pcc = New PerformanceCounterCategory(categoryName)
        End If

        ' Get the counters for this instance or a single instance 
        ' of the selected category.
        If instanceName.Length > 0 Then
            counters = pcc.GetCounters(instanceName)
        Else
            counters = pcc.GetCounters()
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to get counter information for " & _
            IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
            "single-instance ") & "category ""{0}"" on " & _
            IIf(machineName.Length > 0, "computer ""{1}"":", _
            "this computer:"), _
            categoryName, machineName, instanceName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    ' Display the counter names if GetCounters was successful.
    If Not counters Is Nothing Then
        Console.WriteLine("These counters exist in " & _
            IIf(instanceName.Length > 0, "instance ""{1}"" of", _
            "single instance") & " category {0} on " & _
            IIf(machineName.Length > 0, _
                "computer ""{2}"":", "this computer:"), _
            categoryName, instanceName, machineName)

        ' Display a numbered list of the counter names.
        Dim objX As Integer
        For objX = 0 To counters.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, _
                counters(objX).CounterName)
        Next objX
    End If
End Sub

Commenti

Per altre informazioni sulle istanze dell'oggetto prestazioni, vedere panoramica della PerformanceCounter classe.

Nota

Per leggere i contatori delle prestazioni da una sessione di accesso non interattiva in Windows Vista e versioni successive, Windows XP Professional x64 Edition o Windows Server 2003, è necessario essere un membro del gruppo utenti Monitor prestazioni o disporre di privilegi amministrativi.

Per evitare di dover elevare i privilegi per accedere ai contatori delle prestazioni in Windows Vista e versioni successive, aggiungere se stessi al gruppo utenti di Monitor prestazioni.

In Windows Vista e versioni successive i privilegi di un utente sono determinati dalla funzionalità Controllo dell'account utente. Ai membri del gruppo Administrators predefinito vengono assegnati due token di accesso in fase di esecuzione, ovvero un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, viene assegnato il ruolo dell'utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è prima necessario elevare i privilegi dall'utente standard all'amministratore. È possibile farlo quando si avvia un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando l'opzione di esecuzione come amministratore.

Vedi anche

Si applica a

GetCounters(String)

Recupera un elenco di contatori in una categoria di contatori delle prestazioni che contiene una o più istanze.

public:
 cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters(System::String ^ instanceName);
public System.Diagnostics.PerformanceCounter[] GetCounters (string instanceName);
member this.GetCounters : string -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters (instanceName As String) As PerformanceCounter()

Parametri

instanceName
String

L'istanza dell'oggetto delle prestazioni per la quale recuperare un elenco di contatori associati.

Restituisce

Una matrice di oggetti PerformanceCounter che indica i contatori associati all'istanza dell'oggetto specificato di questa categoria di contatori delle prestazioni.

Eccezioni

Il valore del parametro instanceName è null.

La proprietà CategoryName di questa istanza di PerformanceCounterCategory non è stata impostata.

-oppure-

La categoria non contiene l'istanza specificata dal parametro instanceName.

Una chiamata ad un'API di sistema sottostante non ha avuto esito positivo.

Codice eseguito senza privilegi di amministratore ha tentato di leggere un contatore delle prestazioni.

Esempio

Nell'esempio PerformanceCounter di codice seguente viene visualizzato un elenco degli oggetti in un PerformanceCounterCategoryoggetto . Crea prima di tutto un oggetto PerformanceCounterCategory con il costruttore appropriato, in base al fatto che sia stato specificato un nome computer. Usa quindi il metodo per restituire una matrice di PerformanceCounter oggetti, selezionando l'overload GetCounters in base al fatto che sia stato specificato un nome dell'istanzaGetCounters.

Questo GetCounters(String) overload non riesce a meno che non venga usato con una categoria contenente istanze.

public:
    static void Main(array<String^>^ args)
    {
        String^ categoryName = "";
        String^ machineName = "";
        String^ instanceName = "";
        PerformanceCounterCategory^ pcc;
        array<PerformanceCounter^>^ counters;

        // Copy the supplied arguments into the local variables.
        try
        {
            categoryName = args[1];
            machineName = args[2]=="."? "": args[2];
            instanceName = args[3];
        }
        catch (...)
        {
            // Ignore the exception from non-supplied arguments.
        }

        try
        {
            // Create the appropriate PerformanceCounterCategory object.
            if (machineName->Length>0)
            {
                pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
            }
            else
            {
                pcc = gcnew PerformanceCounterCategory(categoryName);
            }

            // Get the counters for this instance or a single instance
            // of the selected category.
            if (instanceName->Length > 0)
            {
                counters = pcc->GetCounters(instanceName);
            }
            else
            {
                counters = pcc->GetCounters();
            }

        }
        catch (Exception^ ex)
        {
            Console::WriteLine("Unable to get counter information for " +
                (instanceName->Length>0? "instance \"{2}\" in ": "single-instance ") +
                "category \"{0}\" on " + (machineName->Length>0? "computer \"{1}\":": "this computer:"),
                categoryName, machineName, instanceName);
            Console::WriteLine(ex->Message);
            return;
        }

        // Display the counter names if GetCounters was successful.
        if (counters != nullptr)
        {
            Console::WriteLine("These counters exist in " +
                (instanceName->Length > 0 ? "instance \"{1}\" of" : "single instance") +
                " category {0} on " + (machineName->Length > 0 ? "computer \"{2}\":": "this computer:"),
                categoryName, instanceName, machineName);

            // Display a numbered list of the counter names.
            int objX;
            for(objX = 0; objX < counters->Length; objX++)
            {
                Console::WriteLine("{0,4} - {1}", objX + 1, counters[objX]->CounterName);
            }
        }
    }
public static void Main(string[] args)
{
    string categoryName = "";
    string machineName = "";
    string instanceName = "";
    PerformanceCounterCategory pcc;
    PerformanceCounter[] counters;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        machineName = args[1]=="."? "": args[1];
        instanceName = args[2];
    }
    catch
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // Create the appropriate PerformanceCounterCategory object.
        if (machineName.Length>0)
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }

        // Get the counters for this instance or a single instance
        // of the selected category.
        if (instanceName.Length>0)
        {
            counters = pcc.GetCounters(instanceName);
        }
        else
        {
            counters = pcc.GetCounters();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get counter information for " +
            (instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
            "category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
            categoryName, machineName, instanceName);
        Console.WriteLine(ex.Message);
        return;
    }

    // Display the counter names if GetCounters was successful.
    if (counters!=null)
    {
        Console.WriteLine("These counters exist in " +
            (instanceName.Length>0? "instance \"{1}\" of": "single instance") +
            " category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
            categoryName, instanceName, machineName);

        // Display a numbered list of the counter names.
        int objX;
        for(objX=0; objX<counters.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim instanceName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim counters() As PerformanceCounter

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        machineName = IIf(args(1) = ".", "", args(1))
        instanceName = args(2)
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Create the appropriate PerformanceCounterCategory object.
        If machineName.Length > 0 Then
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        Else
            pcc = New PerformanceCounterCategory(categoryName)
        End If

        ' Get the counters for this instance or a single instance 
        ' of the selected category.
        If instanceName.Length > 0 Then
            counters = pcc.GetCounters(instanceName)
        Else
            counters = pcc.GetCounters()
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to get counter information for " & _
            IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
            "single-instance ") & "category ""{0}"" on " & _
            IIf(machineName.Length > 0, "computer ""{1}"":", _
            "this computer:"), _
            categoryName, machineName, instanceName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    ' Display the counter names if GetCounters was successful.
    If Not counters Is Nothing Then
        Console.WriteLine("These counters exist in " & _
            IIf(instanceName.Length > 0, "instance ""{1}"" of", _
            "single instance") & " category {0} on " & _
            IIf(machineName.Length > 0, _
                "computer ""{2}"":", "this computer:"), _
            categoryName, instanceName, machineName)

        ' Display a numbered list of the counter names.
        Dim objX As Integer
        For objX = 0 To counters.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, _
                counters(objX).CounterName)
        Next objX
    End If
End Sub

Commenti

Per rappresentare una categoria a istanza singola, passare una stringa vuota ("") per il instanceName parametro.

Per altre informazioni sulle istanze dell'oggetto prestazioni, vedere panoramica della PerformanceCounter classe.

Nota

Per leggere i contatori delle prestazioni da una sessione di accesso non interattiva in Windows Vista e versioni successive, Windows XP Professional x64 Edition o Windows Server 2003, è necessario essere un membro del gruppo utenti Monitor prestazioni o disporre di privilegi amministrativi.

Per evitare di dover elevare i privilegi per accedere ai contatori delle prestazioni in Windows Vista e versioni successive, aggiungere se stessi al gruppo utenti di Monitor prestazioni.

In Windows Vista e versioni successive i privilegi di un utente sono determinati dalla funzionalità Controllo dell'account utente. Ai membri del gruppo Administrators predefinito vengono assegnati due token di accesso in fase di esecuzione, ovvero un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, viene assegnato il ruolo dell'utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è prima necessario elevare i privilegi dall'utente standard all'amministratore. È possibile farlo quando si avvia un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando l'opzione di esecuzione come amministratore.

Vedi anche

Si applica a