PerformanceCounterCategory.GetInstanceNames Metodo

Definizione

Recupera l'elenco di istanze dell'oggetto delle prestazioni associate a questa categoria.

public:
 cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames ();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()

Restituisce

String[]

Una matrice di stringhe che rappresentano i nomi delle istanze degli oggetti delle prestazioni associate a questa categoria oppure, se la categoria contiene soltanto un'istanza dell'oggetto delle prestazioni, una matrice a una sola voce che contiene una stringa vuota ("").

Eccezioni

La proprietà CategoryName è null. È probabile che la proprietà non sia stata impostata.

-oppure-

Alla categoria non è associata un'istanza.

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 restituito un elenco degli oggetti in un oggetto PerformanceCounterCategory. Crea innanzitutto un PerformanceCounterCategory oggetto utilizzando il costruttore appropriato in base al fatto che sia stato specificato un nome computer. Viene quindi usato GetInstanceNames per restituire i nomi di istanza come matrice di String, che ordina e visualizza.

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

        // Copy the supplied arguments into the local variables.
        try
        {
            categoryName = args[1];
            machineName = args[2]=="."? "": args[2];
        }
        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 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]);
            }
        }
    }
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

Commenti

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 membri del gruppo utenti di 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 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, è necessario innanzitutto 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.

Si applica a

Vedi anche