PerformanceCounterCategory.GetCategories 方法

定义

检索计算机上注册的性能计数器类别的列表。

重载

GetCategories()

检索本地计算机上注册的性能计数器类别的列表。

GetCategories(String)

检索指定计算机上注册的性能计数器类别的列表。

GetCategories()

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

检索本地计算机上注册的性能计数器类别的列表。

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()

返回

PerformanceCounterCategory 对象的数组,这些对象指示本地计算机上注册的类别。

例外

对基础系统 API 的调用失败。

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。

示例

下面的代码示例使用 GetCategories 方法从本地计算机或指定计算机返回对象的数组 PerformanceCounterCategory 。 它将数组转换为 PerformanceCounterCategory 类别名称数组,为用户排序和显示这些名称。 根据 GetCategories 是否指定了计算机名称来选择重载。

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

注解

注意

若要从 Windows Vista 及更高版本、Windows XP Professional x64 Edition 或 Windows Server 2003 中的非交互式登录会话读取性能计数器,你必须是 性能监视器 用户组的成员或具有管理权限。

若要避免在 Windows Vista 及更高版本中提升访问性能计数器的权限,请将自己添加到 性能监视器 用户组。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于

GetCategories(String)

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

检索指定计算机上注册的性能计数器类别的列表。

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()

参数

machineName
String

要查看的计算机。

返回

PerformanceCounterCategory 对象的数组,这些对象指示指定计算机上注册的类别。

例外

machineName 参数无效。

对基础系统 API 的调用失败。

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。

示例

下面的代码示例使用 GetCategories 方法从本地计算机或指定计算机返回对象的数组 PerformanceCounterCategory 。 它将数组转换为 PerformanceCounterCategory 类别名称数组,为用户排序和显示这些名称。 根据 GetCategories 是否指定了计算机名称来选择重载。

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

注解

若要检索本地计算机上的类别,请使用另一个重载或传递“.”作为 machineName 参数。

注意

若要从 Windows Vista 及更高版本、Windows XP Professional x64 Edition 或 Windows Server 2003 中的非交互式登录会话读取性能计数器,你必须是 性能监视器 用户组的成员或具有管理权限。

若要避免在 Windows Vista 及更高版本中提升访问性能计数器的权限,请将自己添加到 性能监视器 用户组。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于