PerformanceCounterCategory.GetInstanceNames Methode

Definitie

Hiermee haalt u de lijst met prestatieobjectexemplaren op die aan deze categorie zijn gekoppeld.

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

Retouren

String[]

Een matrix met tekenreeksen die de namen van de prestatieobjectexemplaren vertegenwoordigen die zijn gekoppeld aan deze categorie of, als de categorie slechts één exemplaar van het prestatieobject bevat, een matrix met één vermelding die een lege tekenreeks bevat ("").

Uitzonderingen

De eigenschap CategoryName is null. De eigenschap is mogelijk niet ingesteld.

– of –

De categorie heeft geen gekoppeld exemplaar.

Een aanroep naar een onderliggende systeem-API is mislukt.

Code die wordt uitgevoerd zonder beheerdersbevoegdheden heeft geprobeerd een prestatiemeteritem te lezen.

Voorbeelden

In het volgende codevoorbeeld wordt een lijst met de PerformanceCounter objecten in een PerformanceCounterCategory. Er wordt eerst een PerformanceCounterCategory object gemaakt met behulp van de juiste constructor op basis van of er een computernaam is opgegeven. Vervolgens wordt GetInstanceNames de instantienamen geretourneerd als een matrix van String, die wordt gesorteerd en weergegeven.

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

Opmerkingen

Note

Als u prestatiemeteritems wilt lezen van een niet-interactieve aanmeldingssessie in Windows Vista en hoger, Windows XP Professional x64 Edition of Windows Server 2003, moet u lid zijn van de groep Performance Monitor Gebruikers of beheerdersbevoegdheden hebben.

Als u wilt voorkomen dat u uw bevoegdheden voor toegang tot prestatiemeteritems in Windows Vista en hoger moet verhogen, voegt u uzelf toe aan de groep Performance Monitor Gebruikers.

In Windows Vista en hoger bepaalt UAC (User Account Control) de bevoegdheden van een gebruiker. Als u lid bent van de groep Ingebouwde beheerders, hebt u twee runtime-toegangstokens toegewezen: een standaard toegangstoken voor gebruikers en een beheerderstoegangstoken. Standaard hebt u de standaardgebruikersrol. Als u de code wilt uitvoeren die toegang heeft tot prestatiemeteritems, moet u eerst uw bevoegdheden verhogen van de standaardgebruiker naar de beheerder. U kunt dit doen wanneer u een toepassing start door met de rechtermuisknop op het toepassingspictogram te klikken en aan te geven dat u wilt uitvoeren als beheerder.

Van toepassing op

Zie ook