PerformanceCounterCategory.GetCounters Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Retrieves a list of the counters in this performance counter category.
Overloads
GetCounters() |
Retrieves a list of the counters in a performance counter category that contains exactly one instance. |
GetCounters(String) |
Retrieves a list of the counters in a performance counter category that contains one or more instances. |
GetCounters()
Retrieves a list of the counters in a performance counter category that contains exactly one instance.
public:
cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters();
public System.Diagnostics.PerformanceCounter[] GetCounters ();
member this.GetCounters : unit -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters () As PerformanceCounter()
Returns
An array of PerformanceCounter objects indicating the counters that are associated with this single-instance performance counter category.
Exceptions
The category is not a single instance.
A call to an underlying system API failed.
The category does not have an associated instance.
Code that is executing without administrative privileges attempted to read a performance counter.
Examples
The following code example gets a list of the PerformanceCounter objects in a PerformanceCounterCategory. It first creates a PerformanceCounterCategory with the appropriate constructor, based on whether a computer name was specified. It then uses the GetCounters method to return an array of PerformanceCounter objects, selecting the GetCounters overload based on whether an instance name was specified.
This GetCounters() overload fails unless it is used with a single-instance category.
public:
static void Main(array<String^>^ args)
{
String^ categoryName = "";
String^ machineName = "";
String^ instanceName = "";
PerformanceCounterCategory^ pcc;
array<PerformanceCounter^>^ counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[1];
machineName = args[2]=="."? "": args[2];
instanceName = args[3];
}
catch (...)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName->Length>0)
{
pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = gcnew 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 != nullptr)
{
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);
}
}
}
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
Remarks
For more information about performance object instances, see the PerformanceCounter class overview.
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
GetCounters(String)
Retrieves a list of the counters in a performance counter category that contains one or more instances.
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()
Parameters
- instanceName
- String
The performance object instance for which to retrieve the list of associated counters.
Returns
An array of PerformanceCounter objects indicating the counters that are associated with the specified object instance of this performance counter category.
Exceptions
The instanceName
parameter is null
.
The CategoryName property for this PerformanceCounterCategory instance has not been set.
-or-
The category does not contain the instance that is specified by the instanceName
parameter.
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 gets a list of the PerformanceCounter objects in a PerformanceCounterCategory. It first creates a PerformanceCounterCategory with the appropriate constructor, based on whether a computer name was specified. It then uses the GetCounters method to return an array of PerformanceCounter objects, selecting the GetCounters overload based on whether an instance name was specified.
This GetCounters(String) overload fails unless it is used with a category that contains instances.
public:
static void Main(array<String^>^ args)
{
String^ categoryName = "";
String^ machineName = "";
String^ instanceName = "";
PerformanceCounterCategory^ pcc;
array<PerformanceCounter^>^ counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[1];
machineName = args[2]=="."? "": args[2];
instanceName = args[3];
}
catch (...)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName->Length>0)
{
pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = gcnew 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 != nullptr)
{
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);
}
}
}
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
Remarks
To represent a single-instance category, pass an empty string ("") for the instanceName
parameter.
For more information about performance object instances, see the PerformanceCounter class overview.
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
.NET