Share via


PerformanceCounterCategory.GetCounters Méthode

Définition

Récupère la liste des compteurs de la catégorie de compteurs de performance.

Surcharges

GetCounters()

Récupère la liste des compteurs d'une catégorie de compteurs de performance qui contient exactement une seule instance.

GetCounters(String)

Récupère la liste des compteurs d'une catégorie de compteurs de performance qui contient une ou plusieurs instances.

GetCounters()

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Récupère la liste des compteurs d'une catégorie de compteurs de performance qui contient exactement une seule instance.

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

Retours

Tableau d'objets PerformanceCounter indiquant les compteurs associés à cette catégorie de compteurs de performance à instance unique.

Exceptions

La catégorie n'est pas une instance unique.

Échec de l'appel à une API système sous-jacente.

La catégorie n'a pas d'instance associée.

Code s'exécutant sans privilèges d'administrateur, destiné à lire un compteur de performance.

Exemples

L’exemple de code suivant obtient une liste des PerformanceCounter objets dans un PerformanceCounterCategory. Il crée d’abord un PerformanceCounterCategory avec le constructeur approprié, selon qu’un nom d’ordinateur a été spécifié ou non. Il utilise ensuite la GetCounters méthode pour retourner un tableau d’objetsPerformanceCounter, en sélectionnant la GetCounters surcharge selon qu’un nom de instance a été spécifié.

Cette GetCounters() surcharge échoue, sauf si elle est utilisée avec une catégorie de instance unique.

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

Remarques

Pour plus d’informations sur les instances d’objet de performances, consultez la vue d’ensemble de la PerformanceCounter classe.

Notes

Pour lire les compteurs de performances d’une session de connexion non interactive dans Windows Vista et versions ultérieures, Windows XP Professionnel Édition x64 ou Windows Server 2003, vous devez être membre du groupe Utilisateurs Analyseur de performances ou disposer de privilèges d’administrateur.

Pour éviter d’avoir à élever vos privilèges pour accéder aux compteurs de performances dans Windows Vista et versions ultérieures, ajoutez-vous au groupe Utilisateurs Analyseur de performances.

Dans Windows Vista et version ultérieure, le contrôle de compte d'utilisateur détermine les privilèges d'un utilisateur. Si vous êtes membre du groupe Administrateurs intégrés, deux jetons d'accès au moment de l'exécution vous sont assignés : un jeton d'accès utilisateur standard et un jeton d'accès administrateur. Par défaut, vous êtes dans le rôle d'utilisateur standard. Pour exécuter le code qui accède aux compteurs de performances, vous devez d’abord élever vos privilèges d’utilisateur standard à administrateur. Vous pouvez effectuer cela au démarrage d'une application en cliquant avec le bouton droit sur l'icône de l'application et en indiquant que vous voulez l'exécuter en tant qu'administrateur.

Voir aussi

S’applique à

GetCounters(String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Récupère la liste des compteurs d'une catégorie de compteurs de performance qui contient une ou plusieurs instances.

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()

Paramètres

instanceName
String

Instance de l'objet de performance pour laquelle récupérer la liste des compteurs associés.

Retours

Tableau d'objets PerformanceCounter indiquant les compteurs associés à l'instance d'objet spécifiée de cette catégorie de compteurs de performance.

Exceptions

Le paramètre instanceName a la valeur null.

La propriété CategoryName de cette instance de PerformanceCounterCategory n'est pas définie.

- ou -

La catégorie ne contient pas l'instance spécifiée par le paramètre instanceName.

Échec de l'appel à une API système sous-jacente.

Code s'exécutant sans privilèges d'administrateur, destiné à lire un compteur de performance.

Exemples

L’exemple de code suivant obtient une liste des PerformanceCounter objets dans un PerformanceCounterCategory. Il crée d’abord un PerformanceCounterCategory avec le constructeur approprié, selon qu’un nom d’ordinateur a été spécifié ou non. Il utilise ensuite la GetCounters méthode pour retourner un tableau d’objetsPerformanceCounter, en sélectionnant la GetCounters surcharge selon qu’un nom de instance a été spécifié.

Cette GetCounters(String) surcharge échoue, sauf si elle est utilisée avec une catégorie qui contient des instances.

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

Remarques

Pour représenter une catégorie instance unique, passez une chaîne vide («  ») pour le instanceName paramètre .

Pour plus d’informations sur les instances d’objet de performances, consultez la vue d’ensemble de la PerformanceCounter classe.

Notes

Pour lire les compteurs de performances d’une session de connexion non interactive dans Windows Vista et versions ultérieures, Windows XP Professionnel Édition x64 ou Windows Server 2003, vous devez être membre du groupe Utilisateurs Analyseur de performances ou disposer de privilèges d’administrateur.

Pour éviter d’avoir à élever vos privilèges pour accéder aux compteurs de performances dans Windows Vista et versions ultérieures, ajoutez-vous au groupe Utilisateurs Analyseur de performances.

Dans Windows Vista et version ultérieure, le contrôle de compte d'utilisateur détermine les privilèges d'un utilisateur. Si vous êtes membre du groupe Administrateurs intégrés, deux jetons d'accès au moment de l'exécution vous sont assignés : un jeton d'accès utilisateur standard et un jeton d'accès administrateur. Par défaut, vous êtes dans le rôle d'utilisateur standard. Pour exécuter le code qui accède aux compteurs de performances, vous devez d’abord élever vos privilèges d’utilisateur standard à administrateur. Vous pouvez effectuer cela au démarrage d'une application en cliquant avec le bouton droit sur l'icône de l'application et en indiquant que vous voulez l'exécuter en tant qu'administrateur.

Voir aussi

S’applique à