Edit

Share via


PerformanceCounterCategory.GetCategories Method

Definition

Retrieves a list of the performance counter categories that are registered on a computer.

Overloads

GetCategories()

Retrieves a list of the performance counter categories that are registered on the local computer.

GetCategories(String)

Retrieves a list of the performance counter categories that are registered on the specified computer.

GetCategories()

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

Retrieves a list of the performance counter categories that are registered on the local computer.

public:
 static cli::array <System::Diagnostics::PerformanceCounterCategory ^> ^ GetCategories();
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories ();
static member GetCategories : unit -> System.Diagnostics.PerformanceCounterCategory[]
Public Shared Function GetCategories () As PerformanceCounterCategory()

Returns

An array of PerformanceCounterCategory objects indicating the categories that are registered on the local computer.

Exceptions

A call to an underlying system API failed.

Code that is executing without administrative privileges attempted to read a performance counter.

Examples

The following code example uses the GetCategories method to return an array of PerformanceCounterCategory objects from the local computer or a specified computer. It converts the PerformanceCounterCategory array into an array of category names, which it sorts and displays for the user. The GetCategories overload is selected based on whether a computer name was specified.

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

        // Copy the machine name argument into the local variable.
        try
        {
            machineName = args[1]=="."? "": args[1];
        }
        catch (...)
        {
        }

        // Generate a list of categories registered on the specified computer.
        try
        {
            if (machineName->Length > 0)
            {
                categories = PerformanceCounterCategory::GetCategories(machineName);
            }
            else
            {
                categories = PerformanceCounterCategory::GetCategories();
            }
        }
        catch(Exception^ ex)
        {
            Console::WriteLine("Unable to get categories on " +
                (machineName->Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
            Console::WriteLine(ex->Message);
            return;
        }

        Console::WriteLine("These categories are registered on " +
            (machineName->Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

        // Create and sort an array of category names.
        array<String^>^ categoryNames = gcnew array<String^>(categories->Length);
        int objX;
        for (objX = 0; objX < categories->Length; objX++)
        {
            categoryNames[objX] = categories[objX]->CategoryName;
        }
        Array::Sort(categoryNames);

        for (objX = 0; objX < categories->Length; objX++)
        {
            Console::WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
        }
    }
public static void Main(string[] args)
{
    string machineName = "";
    PerformanceCounterCategory[] categories;

    // Copy the machine name argument into the local variable.
    try
    {
        machineName = args[0]=="."? "": args[0];
    }
    catch
    {
    }

    // Generate a list of categories registered on the specified computer.
    try
    {
        if (machineName.Length > 0)
        {
            categories = PerformanceCounterCategory.GetCategories(machineName);
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get categories on " +
            (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    Console.WriteLine("These categories are registered on " +
        (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

    // Create and sort an array of category names.
    string[] categoryNames = new string[categories.Length];
    int objX;
    for(objX = 0; objX < categories.Length; objX++)
    {
        categoryNames[objX] = categories[objX].CategoryName;
    }
    Array.Sort(categoryNames);

    for(objX = 0; objX < categories.Length; objX++)
    {
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
    }
}
Sub Main(ByVal args() As String)
    Dim machineName As String = ""
    Dim categories() As PerformanceCounterCategory

    ' Copy the machine name argument into the local variable.
    Try
        machineName = IIf(args(0) = ".", "", args(0))
    Catch ex As Exception
    End Try

    ' Generate a list of categories registered on the specified computer.
    Try
        If machineName.Length > 0 Then
            categories = _
                PerformanceCounterCategory.GetCategories(machineName)
        Else
            categories = PerformanceCounterCategory.GetCategories()
        End If
    Catch ex As Exception
        Console.WriteLine("Unable to get categories on " & _
            IIf(machineName.Length > 0, "computer ""{0}"":", _
            "this computer:"), machineName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    Console.WriteLine("These categories are registered on " & _
        IIf(machineName.Length > 0, _
            "computer ""{0}"":", "this computer:"), machineName)

    ' Create and sort an array of category names.
    Dim categoryNames(categories.Length - 1) As String
    Dim objX As Integer
    For objX = 0 To categories.Length - 1
        Console.WriteLine("{0}, {1}", objX, categories(objX).CategoryName)
        categoryNames(objX) = categories(objX).CategoryName
    Next objX
    Array.Sort(categoryNames)

    For objX = 0 To categories.Length - 1
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames(objX))
    Next objX
End Sub

Remarks

Note

To read performance counters from a non-interactive logon session in Windows Vista and later, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

To avoid having to elevate your privileges to access performance counters in Windows Vista and later, add yourself to the Performance Monitor Users group.

In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

See also

Applies to

GetCategories(String)

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

Retrieves a list of the performance counter categories that are registered on the specified computer.

public:
 static cli::array <System::Diagnostics::PerformanceCounterCategory ^> ^ GetCategories(System::String ^ machineName);
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories (string machineName);
static member GetCategories : string -> System.Diagnostics.PerformanceCounterCategory[]
Public Shared Function GetCategories (machineName As String) As PerformanceCounterCategory()

Parameters

machineName
String

The computer to look on.

Returns

An array of PerformanceCounterCategory objects indicating the categories that are registered on the specified computer.

Exceptions

The machineName parameter is invalid.

A call to an underlying system API failed.

Code that is executing without administrative privileges attempted to read a performance counter.

Examples

The following code example uses the GetCategories method to return an array of PerformanceCounterCategory objects from the local computer or a specified computer. It converts the PerformanceCounterCategory array into an array of category names, which it sorts and displays for the user. The GetCategories overload is selected based on whether a computer name was specified.

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

        // Copy the machine name argument into the local variable.
        try
        {
            machineName = args[1]=="."? "": args[1];
        }
        catch (...)
        {
        }

        // Generate a list of categories registered on the specified computer.
        try
        {
            if (machineName->Length > 0)
            {
                categories = PerformanceCounterCategory::GetCategories(machineName);
            }
            else
            {
                categories = PerformanceCounterCategory::GetCategories();
            }
        }
        catch(Exception^ ex)
        {
            Console::WriteLine("Unable to get categories on " +
                (machineName->Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
            Console::WriteLine(ex->Message);
            return;
        }

        Console::WriteLine("These categories are registered on " +
            (machineName->Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

        // Create and sort an array of category names.
        array<String^>^ categoryNames = gcnew array<String^>(categories->Length);
        int objX;
        for (objX = 0; objX < categories->Length; objX++)
        {
            categoryNames[objX] = categories[objX]->CategoryName;
        }
        Array::Sort(categoryNames);

        for (objX = 0; objX < categories->Length; objX++)
        {
            Console::WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
        }
    }
public static void Main(string[] args)
{
    string machineName = "";
    PerformanceCounterCategory[] categories;

    // Copy the machine name argument into the local variable.
    try
    {
        machineName = args[0]=="."? "": args[0];
    }
    catch
    {
    }

    // Generate a list of categories registered on the specified computer.
    try
    {
        if (machineName.Length > 0)
        {
            categories = PerformanceCounterCategory.GetCategories(machineName);
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get categories on " +
            (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    Console.WriteLine("These categories are registered on " +
        (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

    // Create and sort an array of category names.
    string[] categoryNames = new string[categories.Length];
    int objX;
    for(objX = 0; objX < categories.Length; objX++)
    {
        categoryNames[objX] = categories[objX].CategoryName;
    }
    Array.Sort(categoryNames);

    for(objX = 0; objX < categories.Length; objX++)
    {
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
    }
}
Sub Main(ByVal args() As String)
    Dim machineName As String = ""
    Dim categories() As PerformanceCounterCategory

    ' Copy the machine name argument into the local variable.
    Try
        machineName = IIf(args(0) = ".", "", args(0))
    Catch ex As Exception
    End Try

    ' Generate a list of categories registered on the specified computer.
    Try
        If machineName.Length > 0 Then
            categories = _
                PerformanceCounterCategory.GetCategories(machineName)
        Else
            categories = PerformanceCounterCategory.GetCategories()
        End If
    Catch ex As Exception
        Console.WriteLine("Unable to get categories on " & _
            IIf(machineName.Length > 0, "computer ""{0}"":", _
            "this computer:"), machineName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    Console.WriteLine("These categories are registered on " & _
        IIf(machineName.Length > 0, _
            "computer ""{0}"":", "this computer:"), machineName)

    ' Create and sort an array of category names.
    Dim categoryNames(categories.Length - 1) As String
    Dim objX As Integer
    For objX = 0 To categories.Length - 1
        Console.WriteLine("{0}, {1}", objX, categories(objX).CategoryName)
        categoryNames(objX) = categories(objX).CategoryName
    Next objX
    Array.Sort(categoryNames)

    For objX = 0 To categories.Length - 1
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames(objX))
    Next objX
End Sub

Remarks

To retrieve the categories on the local computer, use another overload or pass "." as the machineName parameter.

Note

To read performance counters from a non-interactive logon session in Windows Vista and later, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

To avoid having to elevate your privileges to access performance counters in Windows Vista and later, add yourself to the Performance Monitor Users group.

In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

See also

Applies to