PerformanceCounterCategory.GetInstanceNames 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.
Récupère la liste des instances d’objet de performances associées à cette catégorie.
public:
cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()
Retours
Tableau de chaînes représentant les noms d’instances d’objet de performance associés à cette catégorie ou, si la catégorie contient une seule instance d’objet de performance, un tableau à entrée unique qui contient une chaîne vide ( » « ).
Exceptions
La CategoryName propriété est null. La propriété n’a peut-être pas été définie.
- ou -
La catégorie n’a pas d’instance associée.
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 obtient une liste des objets d’un PerformanceCounterPerformanceCounterCategory. 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 GetInstanceNames ensuite pour renvoyer les noms d’instance en tant que tableau de String, qu’il trie et affiche.
public static void Main(string[] args)
{
string categoryName = "";
string machineName = "";
PerformanceCounterCategory pcc;
string[] instances;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
machineName = args[1]=="."? "": args[1];
}
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 instances associated with this category.
instances = pcc.GetInstanceNames();
}
catch(Exception ex)
{
Console.WriteLine("Unable to get instance information for " +
"category \"{0}\" on " +
(machineName.Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName);
Console.WriteLine(ex.Message);
return;
}
//If an empty array is returned, the category has a single instance.
if (instances.Length==0)
{
Console.WriteLine("Category \"{0}\" on " +
(machineName.Length>0? "computer \"{1}\"": "this computer") +
" is single-instance.", pcc.CategoryName, pcc.MachineName);
}
else
{
// Otherwise, display the instances.
Console.WriteLine("These instances exist in category \"{0}\" on " +
(machineName.Length>0? "computer \"{1}\".": "this computer:"),
pcc.CategoryName, pcc.MachineName);
Array.Sort(instances);
int objX;
for(objX=0; objX<instances.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX+1, instances[objX]);
}
}
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim machineName As String = ""
Dim pcc As PerformanceCounterCategory
Dim instances() As String
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
machineName = IIf(args(1) = ".", "", args(1))
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 instances associated with this category.
instances = pcc.GetInstanceNames()
Catch ex As Exception
Console.WriteLine("Unable to get instance information for " & _
"category ""{0}"" on " & IIf(machineName.Length > 0, _
"computer ""{1}"":", "this computer:"), _
categoryName, machineName)
Console.WriteLine(ex.Message)
Return
End Try
'If an empty array is returned, the category has a single instance.
If instances.Length = 0 Then
Console.WriteLine( _
"Category ""{0}"" on " & IIf(machineName.Length > 0, _
"computer ""{1}""", "this computer") & _
" is single-instance.", pcc.CategoryName, pcc.MachineName)
Else
' Otherwise, display the instances.
Console.WriteLine( _
"These instances exist in category ""{0}"" on " & _
IIf(machineName.Length > 0, _
"computer ""{1}"".", "this computer:"), _
pcc.CategoryName, pcc.MachineName)
Array.Sort(instances)
Dim objX As Integer
For objX = 0 To instances.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, instances(objX))
Next objX
End If
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.