语言

PerformanceCounterCategory.GetInstanceNames 方法

定义

检索与此类别关联的性能对象实例的列表。

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

返回

String[]

表示与此类别关联的性能对象实例名称的字符串数组;如果该类别仅包含一个性能对象实例,则表示包含空字符串(“)的单条目数组。

例外

属性 CategoryName 为 null. 该属性可能尚未设置。

-或-

该类别没有关联的实例。

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

在没有管理权限的情况下执行的代码尝试读取性能计数器。

示例

下面的代码示例获取一个PerformanceCounter对象列表PerformanceCounterCategory。 它首先根据是否指定了计算机名称,使用适当的构造函数创建一个 PerformanceCounterCategory 对象。 然后,它用于 GetInstanceNames 将实例名称作为数组 String返回,它对其进行排序和显示。

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

注解

注释

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

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

在 Windows Vista 及更高版本中,用户帐户控制(UAC)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。

适用于

另请参阅