PerformanceCounterCategory.GetCounters 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索此性能计数器类别中的计数器列表。
重载
| 名称 | 说明 |
|---|---|
| GetCounters() |
检索性能计数器类别中仅包含一个实例的计数器列表。 |
| GetCounters(String) |
检索性能计数器类别中包含一个或多个实例的计数器列表。 |
GetCounters()
检索性能计数器类别中仅包含一个实例的计数器列表。
public:
cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters();
public System.Diagnostics.PerformanceCounter[] GetCounters();
member this.GetCounters : unit -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters () As PerformanceCounter()
返回
一个 PerformanceCounter 对象数组,指示与此单实例性能计数器类别关联的计数器。
例外
类别不是单个实例。
对基础系统 API 的调用失败。
该类别没有关联的实例。
在没有管理权限的情况下执行的代码尝试读取性能计数器。
示例
下面的代码示例获取一个PerformanceCounter对象列表PerformanceCounterCategory。 它首先根据是否指定了计算机名称,使用适当的构造函数创建 PerformanceCounterCategory 一个。 然后,它使用 GetCounters 该方法返回对象数组 PerformanceCounter ,根据是否指定了实例名称来 GetCounters 选择重载。
除非它与单实例类别一起使用,否则此 GetCounters() 重载将失败。
public static void Main(string[] args)
{
string categoryName = "";
string machineName = "";
string instanceName = "";
PerformanceCounterCategory pcc;
PerformanceCounter[] counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
machineName = args[1]=="."? "": args[1];
instanceName = args[2];
}
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 counters for this instance or a single instance
// of the selected category.
if (instanceName.Length>0)
{
counters = pcc.GetCounters(instanceName);
}
else
{
counters = pcc.GetCounters();
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to get counter information for " +
(instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console.WriteLine(ex.Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters!=null)
{
Console.WriteLine("These counters exist in " +
(instanceName.Length>0? "instance \"{1}\" of": "single instance") +
" category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX=0; objX<counters.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
}
}
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim machineName As String = ""
Dim instanceName As String = ""
Dim pcc As PerformanceCounterCategory
Dim counters() As PerformanceCounter
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
machineName = IIf(args(1) = ".", "", args(1))
instanceName = args(2)
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 counters for this instance or a single instance
' of the selected category.
If instanceName.Length > 0 Then
counters = pcc.GetCounters(instanceName)
Else
counters = pcc.GetCounters()
End If
Catch ex As Exception
Console.WriteLine("Unable to get counter information for " & _
IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
"single-instance ") & "category ""{0}"" on " & _
IIf(machineName.Length > 0, "computer ""{1}"":", _
"this computer:"), _
categoryName, machineName, instanceName)
Console.WriteLine(ex.Message)
Return
End Try
' Display the counter names if GetCounters was successful.
If Not counters Is Nothing Then
Console.WriteLine("These counters exist in " & _
IIf(instanceName.Length > 0, "instance ""{1}"" of", _
"single instance") & " category {0} on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"":", "this computer:"), _
categoryName, instanceName, machineName)
' Display a numbered list of the counter names.
Dim objX As Integer
For objX = 0 To counters.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, _
counters(objX).CounterName)
Next objX
End If
End Sub
注解
有关性能对象实例的详细信息,请参阅 PerformanceCounter 类概述。
注释
若要从 Windows Vista 及更高版本中的非交互式登录会话读取性能计数器、Windows XP Professional x64 Edition 或 Windows Server 2003,你必须是性能监视器用户组的成员或具有管理权限。
若要避免提升特权以访问 Windows Vista 及更高版本中的性能计数器,请将自己添加到性能监视器用户组。
在 Windows Vista 及更高版本中,用户帐户控制(UAC)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。
另请参阅
适用于
GetCounters(String)
检索性能计数器类别中包含一个或多个实例的计数器列表。
public:
cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters(System::String ^ instanceName);
public System.Diagnostics.PerformanceCounter[] GetCounters(string instanceName);
member this.GetCounters : string -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters (instanceName As String) As PerformanceCounter()
参数
- instanceName
- String
要为其检索关联计数器列表的性能对象实例。
返回
一个 PerformanceCounter 对象数组,指示与此性能计数器类别的指定对象实例关联的计数器。
例外
参数 instanceName 为 null.
对基础系统 API 的调用失败。
在没有管理权限的情况下执行的代码尝试读取性能计数器。
示例
下面的代码示例获取一个PerformanceCounter对象列表PerformanceCounterCategory。 它首先根据是否指定了计算机名称,使用适当的构造函数创建 PerformanceCounterCategory 一个。 然后,它使用 GetCounters 该方法返回对象数组 PerformanceCounter ,根据是否指定了实例名称来 GetCounters 选择重载。
除非它与包含实例的类别一起使用,否则此 GetCounters(String) 重载将失败。
public static void Main(string[] args)
{
string categoryName = "";
string machineName = "";
string instanceName = "";
PerformanceCounterCategory pcc;
PerformanceCounter[] counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
machineName = args[1]=="."? "": args[1];
instanceName = args[2];
}
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 counters for this instance or a single instance
// of the selected category.
if (instanceName.Length>0)
{
counters = pcc.GetCounters(instanceName);
}
else
{
counters = pcc.GetCounters();
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to get counter information for " +
(instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console.WriteLine(ex.Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters!=null)
{
Console.WriteLine("These counters exist in " +
(instanceName.Length>0? "instance \"{1}\" of": "single instance") +
" category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX=0; objX<counters.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
}
}
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim machineName As String = ""
Dim instanceName As String = ""
Dim pcc As PerformanceCounterCategory
Dim counters() As PerformanceCounter
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
machineName = IIf(args(1) = ".", "", args(1))
instanceName = args(2)
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 counters for this instance or a single instance
' of the selected category.
If instanceName.Length > 0 Then
counters = pcc.GetCounters(instanceName)
Else
counters = pcc.GetCounters()
End If
Catch ex As Exception
Console.WriteLine("Unable to get counter information for " & _
IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
"single-instance ") & "category ""{0}"" on " & _
IIf(machineName.Length > 0, "computer ""{1}"":", _
"this computer:"), _
categoryName, machineName, instanceName)
Console.WriteLine(ex.Message)
Return
End Try
' Display the counter names if GetCounters was successful.
If Not counters Is Nothing Then
Console.WriteLine("These counters exist in " & _
IIf(instanceName.Length > 0, "instance ""{1}"" of", _
"single instance") & " category {0} on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"":", "this computer:"), _
categoryName, instanceName, machineName)
' Display a numbered list of the counter names.
Dim objX As Integer
For objX = 0 To counters.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, _
counters(objX).CounterName)
Next objX
End If
End Sub
注解
若要表示单实例类别,请传递参数的 instanceName 空字符串 (“”)。
有关性能对象实例的详细信息,请参阅 PerformanceCounter 类概述。
注释
若要从 Windows Vista 及更高版本中的非交互式登录会话读取性能计数器、Windows XP Professional x64 Edition 或 Windows Server 2003,你必须是性能监视器用户组的成员或具有管理权限。
若要避免提升特权以访问 Windows Vista 及更高版本中的性能计数器,请将自己添加到性能监视器用户组。
在 Windows Vista 及更高版本中,用户帐户控制(UAC)确定用户的权限。 如果你是内置管理员组的成员,则分配有两个运行时访问令牌:标准用户访问令牌和管理员访问令牌。 默认情况下,你处于标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 在启动应用程序时,可以右键单击应用程序图标并指示要以管理员身份运行。