PerformanceCounterCategory.CounterExists Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Détermine si un compteur spécifié est inscrit dans une catégorie particulière.
Surcharges
| Nom | Description |
|---|---|
| CounterExists(String) |
Détermine si le compteur spécifié est inscrit dans cette catégorie, qui est indiqué par les propriétés et MachineName les CategoryName propriétés. |
| CounterExists(String, String) |
Détermine si le compteur spécifié est inscrit dans la catégorie spécifiée sur l’ordinateur local. |
| CounterExists(String, String, String) |
Détermine si le compteur spécifié est inscrit dans la catégorie spécifiée sur un ordinateur distant. |
CounterExists(String)
Détermine si le compteur spécifié est inscrit dans cette catégorie, qui est indiqué par les propriétés et MachineName les CategoryName propriétés.
public:
bool CounterExists(System::String ^ counterName);
public bool CounterExists(string counterName);
member this.CounterExists : string -> bool
Public Function CounterExists (counterName As String) As Boolean
Paramètres
- counterName
- String
Nom du compteur de performances à rechercher.
Retours
truesi le compteur est inscrit à la catégorie spécifiée par les propriétés et MachineName les CategoryName propriétés ; sinon, false.
Exceptions
counterName a la valeur null.
La CategoryName propriété n’a pas été définie.
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 un PerformanceCounter élément existe. Il obtient un nom de catégorie, un nom de compteur et un nom d’ordinateur à partir de la ligne de commande, s’ils sont donnés. Il crée un PerformanceCounterCategory objet à l’aide de l’élément approprié PerformanceCounterCategory. Il utilise ensuite la CounterExists(String) méthode pour déterminer si le paramètre spécifié PerformanceCounter existe et informe l’utilisateur.
public static void Main(string[] args)
{
string categoryName = "";
string counterName = "";
string machineName = "";
bool objectExists = false;
PerformanceCounterCategory pcc;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
counterName = args[1];
machineName = (args[2]=="."? "": args[2]);
}
catch(Exception ex)
{
// Ignore the exception from non-supplied arguments.
}
try
{
if (machineName.Length==0)
{
pcc = new PerformanceCounterCategory(categoryName);
}
else
{
pcc = new PerformanceCounterCategory(categoryName, machineName);
}
// Check whether the specified counter exists.
// Use the per-instance overload of CounterExists.
objectExists = pcc.CounterExists(counterName);
}
catch(Exception ex)
{
Console.WriteLine("Unable to check for the existence of " +
"counter \"{0}\" in category \"{1}\" on "+
(machineName.Length>0? "computer \"{2}\".": "this computer.")+ "\n" +
ex.Message, counterName, categoryName, machineName);
return;
}
// Tell the user whether the counter exists.
Console.WriteLine("Counter \"{0}\" " + (objectExists? "exists": "does not exist") +
" in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
counterName, pcc.CategoryName, pcc.MachineName);
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim counterName As String = ""
Dim machineName As String = ""
Dim objectExists As Boolean = False
Dim pcc As PerformanceCounterCategory
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
counterName = args(1)
machineName = IIf(args(2) = ".", "", args(2))
Catch ex As Exception
' Ignore the exception from non-supplied arguments.
End Try
Try
If machineName.Length = 0 Then
pcc = New PerformanceCounterCategory(categoryName)
Else
pcc = New PerformanceCounterCategory(categoryName, machineName)
End If
' Check whether the specified counter exists.
' Use the per-instance overload of CounterExists.
objectExists = pcc.CounterExists(counterName)
Catch ex As Exception
Console.WriteLine("Unable to check for the existence of " & _
"counter ""{0}"" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer.") & vbCrLf & _
ex.Message, counterName, categoryName, machineName)
Return
End Try
' Tell the user whether the counter exists.
Console.WriteLine("Counter ""{0}"" " & _
IIf(objectExists, "exists", "does not exist") & _
" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer."), _
counterName, pcc.CategoryName, pcc.MachineName)
End Sub
Remarques
Vous devez définir la propriété avant d’appeler CategoryName cette méthode. Sinon, une exception est levée.
Si vous n’avez pas défini la MachineName propriété, cette méthode utilise 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 à
CounterExists(String, String)
Détermine si le compteur spécifié est inscrit dans la catégorie spécifiée sur l’ordinateur local.
public:
static bool CounterExists(System::String ^ counterName, System::String ^ categoryName);
public static bool CounterExists(string counterName, string categoryName);
static member CounterExists : string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String) As Boolean
Paramètres
- counterName
- String
Nom du compteur de performances à rechercher.
- categoryName
- String
Nom de la catégorie de compteurs de performances ou de l’objet de performance avec lequel le compteur de performances spécifié est associé.
Retours
true, si le compteur est inscrit dans la catégorie spécifiée sur l’ordinateur local ; sinon, false.
Exceptions
Il categoryName s’agit d’une chaîne vide (« »).
Le nom de catégorie n’existe pas.
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 un PerformanceCounter élément existe. Il obtient un nom de catégorie, un nom de compteur et un nom d’ordinateur à partir de la ligne de commande, s’ils sont donnés. Il utilise les surcharges statiques de la CounterExists méthode pour déterminer si le nom spécifié PerformanceCounter existe dans le PerformanceCounterCategory. La surcharge est sélectionnée selon qu’un nom d’ordinateur est fourni.
public static void Main(string[] args)
{
string categoryName = "";
string counterName = "";
string machineName = "";
bool objectExists = false;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
counterName = args[1];
machineName = args[2]=="."? "": args[2];
}
catch(Exception ex)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Check whether the specified counter exists.
// Use the static forms of the CounterExists method.
if (machineName.Length==0)
{
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
}
else
{
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to check for the existence of " +
"counter \"{0}\" in category \"{1}\" on " +
(machineName.Length>0? "computer \"{2}\".": "this computer.") + "\n" +
ex.Message, counterName, categoryName, machineName);
return;
}
// Tell the user whether the counter exists.
Console.WriteLine("Counter \"{0}\" "+ (objectExists? "exists": "does not exist") +
" in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
counterName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim counterName As String = ""
Dim machineName As String = ""
Dim objectExists As Boolean = False
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
counterName = args(1)
machineName = IIf(args(2) = ".", "", args(2))
Catch ex As Exception
' Ignore the exception from non-supplied arguments.
End Try
Try
' Check whether the specified counter exists.
' Use the static forms of the CounterExists method.
If machineName.Length = 0 Then
objectExists = PerformanceCounterCategory.CounterExists( _
counterName, categoryName)
Else
objectExists = PerformanceCounterCategory.CounterExists( _
counterName, categoryName, machineName)
End If
Catch ex As Exception
Console.WriteLine("Unable to check for the existence of " & _
"counter ""{0}"" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer.") & vbCrLf & _
ex.Message, counterName, categoryName, machineName)
Return
End Try
' Tell the user whether the counter exists.
Console.WriteLine("Counter ""{0}"" " & _
IIf(objectExists, "exists", "does not exist") & _
" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer."), _
counterName, categoryName, machineName)
End Sub
Remarques
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 à
CounterExists(String, String, String)
Détermine si le compteur spécifié est inscrit dans la catégorie spécifiée sur un ordinateur distant.
public:
static bool CounterExists(System::String ^ counterName, System::String ^ categoryName, System::String ^ machineName);
public static bool CounterExists(string counterName, string categoryName, string machineName);
static member CounterExists : string * string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String, machineName As String) As Boolean
Paramètres
- counterName
- String
Nom du compteur de performances à rechercher.
- categoryName
- String
Nom de la catégorie de compteurs de performances ou de l’objet de performance avec lequel le compteur de performances spécifié est associé.
- machineName
- String
Nom de l’ordinateur sur lequel se trouvent la catégorie de compteurs de performances et ses compteurs associés.
Retours
true, si le compteur est inscrit dans la catégorie spécifiée sur l’ordinateur spécifié ; sinon, false.
Exceptions
Il categoryName s’agit d’une chaîne vide (« »).
- ou -
La machineName valeur n’est pas valide.
Le nom de catégorie n’existe pas.
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 un PerformanceCounter élément existe. Il obtient un nom de catégorie, un nom de compteur et un nom d’ordinateur à partir de la ligne de commande, s’ils sont donnés. Il utilise les surcharges statiques de la CounterExists méthode pour déterminer si le nom spécifié PerformanceCounter existe dans le PerformanceCounterCategory. La surcharge est sélectionnée selon qu’un nom d’ordinateur est fourni.
public static void Main(string[] args)
{
string categoryName = "";
string counterName = "";
string machineName = "";
bool objectExists = false;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
counterName = args[1];
machineName = args[2]=="."? "": args[2];
}
catch(Exception ex)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Check whether the specified counter exists.
// Use the static forms of the CounterExists method.
if (machineName.Length==0)
{
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
}
else
{
objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to check for the existence of " +
"counter \"{0}\" in category \"{1}\" on " +
(machineName.Length>0? "computer \"{2}\".": "this computer.") + "\n" +
ex.Message, counterName, categoryName, machineName);
return;
}
// Tell the user whether the counter exists.
Console.WriteLine("Counter \"{0}\" "+ (objectExists? "exists": "does not exist") +
" in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
counterName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim counterName As String = ""
Dim machineName As String = ""
Dim objectExists As Boolean = False
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
counterName = args(1)
machineName = IIf(args(2) = ".", "", args(2))
Catch ex As Exception
' Ignore the exception from non-supplied arguments.
End Try
Try
' Check whether the specified counter exists.
' Use the static forms of the CounterExists method.
If machineName.Length = 0 Then
objectExists = PerformanceCounterCategory.CounterExists( _
counterName, categoryName)
Else
objectExists = PerformanceCounterCategory.CounterExists( _
counterName, categoryName, machineName)
End If
Catch ex As Exception
Console.WriteLine("Unable to check for the existence of " & _
"counter ""{0}"" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer.") & vbCrLf & _
ex.Message, counterName, categoryName, machineName)
Return
End Try
' Tell the user whether the counter exists.
Console.WriteLine("Counter ""{0}"" " & _
IIf(objectExists, "exists", "does not exist") & _
" in category ""{1}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"".", "this computer."), _
counterName, categoryName, machineName)
End Sub
Remarques
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.