Partager via


PerformanceCounterCategory.InstanceExists Méthode

Définition

Détermine si la catégorie contient l’instance d’objet de performance spécifiée.

Surcharges

Nom Description
InstanceExists(String)

Détermine si l’instance d’objet de performance spécifiée existe dans la catégorie identifiée par la propriété de CategoryName cet PerformanceCounterCategory objet.

InstanceExists(String, String)

Détermine si une catégorie spécifiée sur l’ordinateur local contient l’instance d’objet de performance spécifiée.

InstanceExists(String, String, String)

Détermine si une catégorie spécifiée sur un ordinateur spécifié contient l’instance d’objet de performance spécifiée.

InstanceExists(String)

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

Détermine si l’instance d’objet de performance spécifiée existe dans la catégorie identifiée par la propriété de CategoryName cet PerformanceCounterCategory objet.

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

Paramètres

instanceName
String

Instance d’objet de performance dans cette catégorie de compteur de performances à rechercher.

Retours

true si la catégorie contient l’instance d’objet de performance spécifiée ; sinon, false.

Exceptions

La CategoryName propriété est null. La propriété n’a peut-être pas été définie.

Le instanceName paramètre est null.

Un appel à une API système sous-jacente a échoué.

Code en cours d’exécution sans privilèges d’administration tenté de lire un compteur de performances.

Exemples

L’exemple de code suivant détermine si une PerformanceCounter instance existe dans un PerformanceCounterCategory. Il crée d’abord un PerformanceCounterCategory objet à l’aide du constructeur approprié en fonction de la spécification d’un nom d’ordinateur. Il utilise InstanceExists(String) ensuite pour déterminer si l’instance spécifiée existe, puis informe l’utilisateur. Si aucun nom d’instance n’est spécifié, l’exemple utilise le nom d’instance unique par défaut.

public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    PerformanceCounterCategory pcc;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

    try
    {
        if (machineName.Length==0)
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }

        // Check whether the instance exists.
        // Use the per-instance overload of InstanceExists.
        objectExists = pcc.InstanceExists(instanceName);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") +
            "\n" + ex.Message, instanceName, categoryName, machineName);
        return;
    }

    // Tell the user whether the instance exists.
    Console.WriteLine("Instance \"{0}\" " + (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        instanceName, pcc.CategoryName, pcc.MachineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim instanceName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False
    Dim pcc As PerformanceCounterCategory
    Const SINGLE_INSTANCE_NAME As String = _
        "systemdiagnosticsperfcounterlibsingleinstance"

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

    Try
        If machineName.Length = 0 Then
            pcc = New PerformanceCounterCategory(categoryName)
        Else
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        End If

        ' Check whether the instance exists.
        ' Use the per-instance overload of InstanceExists.
        objectExists = pcc.InstanceExists(instanceName)

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "instance ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"":", "this computer:") & vbCrLf & _
            ex.Message, instanceName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the instance exists.
    Console.WriteLine("Instance ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        instanceName, pcc.CategoryName, pcc.MachineName)
End Sub

Remarques

Cette surcharge n’est InstanceExists pas static. Il vous oblige à créer un PerformanceCounterCategory objet et à définir la CategoryName propriété.

Note

Pour lire les compteurs de performances à partir d’une session d’ouverture de session non interactive dans Windows Vista et versions ultérieures, Windows XP Professional x64 Edition ou Windows Server 2003, vous devez être membre du groupe Utilisateurs de l’Analyseur de performances ou disposer de privilèges d’administration.

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 de l’Analyseur de performances.

Dans Windows Vista et versions ultérieures, le contrôle de compte d’utilisateur (UAC) détermine les privilèges d’un utilisateur. Si vous êtes membre du groupe Administrateurs intégrés, vous disposez de deux jetons d’accès au moment de l’exécution : 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 de l’utilisateur standard à l’administrateur. Pour ce faire, lorsque vous démarrez une application, cliquez avec le bouton droit sur l’icône de l’application et indiquez que vous souhaitez exécuter en tant qu’administrateur.

Voir aussi

S’applique à

InstanceExists(String, String)

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

Détermine si une catégorie spécifiée sur l’ordinateur local contient l’instance d’objet de performance spécifiée.

public:
 static bool InstanceExists(System::String ^ instanceName, System::String ^ categoryName);
public static bool InstanceExists(string instanceName, string categoryName);
static member InstanceExists : string * string -> bool
Public Shared Function InstanceExists (instanceName As String, categoryName As String) As Boolean

Paramètres

instanceName
String

Instance d’objet de performance à rechercher.

categoryName
String

Catégorie de compteur de performances à rechercher.

Retours

true si la catégorie contient l’instance d’objet de performance spécifiée ; sinon, false.

Exceptions

Le instanceName paramètre est null.

- ou -

Le categoryName paramètre est null.

Le categoryName paramètre est une chaîne vide («  »).

Un appel à une API système sous-jacente a échoué.

Code en cours d’exécution sans privilèges d’administration tenté de lire un compteur de performances.

Exemples

L’exemple de code suivant utilise les surcharges statiques pour InstanceExists déterminer si l’instance donnée PerformanceCounter existe dans le PerformanceCounterCategory. La surcharge est sélectionnée en fonction de la spécification d’un nom d’ordinateur. Si aucun nom d’instance n’est spécifié, l’exemple utilise le nom d’instance unique par défaut.

public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

    try
    {
        // Check whether the specified instance exists.
        // Use the static forms of the InstanceExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") + "\n" +
            ex.Message, instanceName, categoryName, machineName);
        return;
    }

    // Tell the user whether the instance exists.
    Console.WriteLine("Instance \"{0}\" " + (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        instanceName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim instanceName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False
    Const SINGLE_INSTANCE_NAME As String = _
        "systemdiagnosticsperfcounterlibsingleinstance"

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

    Try
        ' Check whether the specified instance exists.
        ' Use the static forms of the InstanceExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.InstanceExists( _
                instanceName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.InstanceExists( _
                instanceName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "instance ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"":", "this computer:") & vbCrLf & _
            ex.Message, instanceName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the instance exists.
    Console.WriteLine("Instance ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        instanceName, categoryName, machineName)
End Sub

Remarques

Il n’est pas possible de déterminer si une instance d’objet de performance existe sur un ordinateur sans spécifier de catégorie spécifique à rechercher.

Note

Pour lire les compteurs de performances à partir d’une session d’ouverture de session non interactive dans Windows Vista et versions ultérieures, Windows XP Professional x64 Edition ou Windows Server 2003, vous devez être membre du groupe Utilisateurs de l’Analyseur de performances ou disposer de privilèges d’administration.

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 de l’Analyseur de performances.

Dans Windows Vista et versions ultérieures, le contrôle de compte d’utilisateur (UAC) détermine les privilèges d’un utilisateur. Si vous êtes membre du groupe Administrateurs intégrés, vous disposez de deux jetons d’accès au moment de l’exécution : 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 de l’utilisateur standard à l’administrateur. Pour ce faire, lorsque vous démarrez une application, cliquez avec le bouton droit sur l’icône de l’application et indiquez que vous souhaitez exécuter en tant qu’administrateur.

Voir aussi

S’applique à

InstanceExists(String, String, String)

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

Détermine si une catégorie spécifiée sur un ordinateur spécifié contient l’instance d’objet de performance spécifiée.

public:
 static bool InstanceExists(System::String ^ instanceName, System::String ^ categoryName, System::String ^ machineName);
public static bool InstanceExists(string instanceName, string categoryName, string machineName);
static member InstanceExists : string * string * string -> bool
Public Shared Function InstanceExists (instanceName As String, categoryName As String, machineName As String) As Boolean

Paramètres

instanceName
String

Instance d’objet de performance à rechercher.

categoryName
String

Catégorie de compteur de performances à rechercher.

machineName
String

Nom de l’ordinateur sur lequel rechercher la paire d’instances de catégorie.

Retours

true si la catégorie contient l’instance d’objet de performance spécifiée ; sinon, false.

Exceptions

Le instanceName paramètre est null.

- ou -

Le categoryName paramètre est null.

Le categoryName paramètre est une chaîne vide («  »).

- ou -

Le machineName paramètre n’est pas valide.

Un appel à une API système sous-jacente a échoué.

Code en cours d’exécution sans privilèges d’administration tenté de lire un compteur de performances.

Exemples

L’exemple de code suivant utilise les surcharges statiques pour InstanceExists déterminer si l’instance donnée PerformanceCounter existe dans le PerformanceCounterCategory. La surcharge est sélectionnée en fonction de la spécification d’un nom d’ordinateur. Si aucun nom d’instance n’est spécifié, l’exemple utilise le nom d’instance unique par défaut.

public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

    try
    {
        // Check whether the specified instance exists.
        // Use the static forms of the InstanceExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") + "\n" +
            ex.Message, instanceName, categoryName, machineName);
        return;
    }

    // Tell the user whether the instance exists.
    Console.WriteLine("Instance \"{0}\" " + (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        instanceName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim instanceName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False
    Const SINGLE_INSTANCE_NAME As String = _
        "systemdiagnosticsperfcounterlibsingleinstance"

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

    Try
        ' Check whether the specified instance exists.
        ' Use the static forms of the InstanceExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.InstanceExists( _
                instanceName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.InstanceExists( _
                instanceName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "instance ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"":", "this computer:") & vbCrLf & _
            ex.Message, instanceName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the instance exists.
    Console.WriteLine("Instance ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        instanceName, categoryName, machineName)
End Sub

Remarques

Il n’est pas possible de déterminer si une instance d’objet de performance existe sur un ordinateur sans spécifier de catégorie spécifique à rechercher.

Vous pouvez utiliser « ». pour spécifier l’ordinateur local.

Note

Pour lire les compteurs de performances à partir d’une session d’ouverture de session non interactive dans Windows Vista et versions ultérieures, Windows XP Professional x64 Edition ou Windows Server 2003, vous devez être membre du groupe Utilisateurs de l’Analyseur de performances ou disposer de privilèges d’administration.

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 de l’Analyseur de performances.

Dans Windows Vista et versions ultérieures, le contrôle de compte d’utilisateur (UAC) détermine les privilèges d’un utilisateur. Si vous êtes membre du groupe Administrateurs intégrés, vous disposez de deux jetons d’accès au moment de l’exécution : 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 de l’utilisateur standard à l’administrateur. Pour ce faire, lorsque vous démarrez une application, cliquez avec le bouton droit sur l’icône de l’application et indiquez que vous souhaitez exécuter en tant qu’administrateur.

Voir aussi

S’applique à