PerformanceCounterCategory.GetInstanceNames 方法
检索与此类别关联的性能对象实例列表。
**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)
语法
声明
Public Function GetInstanceNames As String()
用法
Dim instance As PerformanceCounterCategory
Dim returnValue As String()
returnValue = instance.GetInstanceNames
public string[] GetInstanceNames ()
public:
array<String^>^ GetInstanceNames ()
public String[] GetInstanceNames ()
public function GetInstanceNames () : String[]
返回值
字符串的数组,这些字符串表示与此类别关联的性能对象实例名称;或者,如果该类别仅包含一个性能对象实例,则为包含空字符串 ("") 的单项数组。
异常
异常类型 | 条件 |
---|---|
CategoryName 属性为 空引用(在 Visual Basic 中为 Nothing)。可能尚未设置该属性。 - 或 - 没有与该类别关联的实例。 |
|
对基础系统 API 的调用失败。 |
示例
下面的代码示例获取 PerformanceCounterCategory 中 PerformanceCounter 对象的列表。它首先根据是否指定了计算机名称,使用相应的构造函数创建 PerformanceCounterCategory 对象。然后使用 GetInstanceNames 将实例名称作为 String 数组返回,接着将对该数组进行排序和显示。
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 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
PerformanceCounterCategory 类
PerformanceCounterCategory 成员
System.Diagnostics 命名空间
CategoryName
PerformanceCounter.InstanceName 属性
PerformanceCounter 类