PerformanceCounterCategory.Exists 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.
Determines whether the category is registered on the system.
Overloads
Exists(String) |
Determines whether the category is registered on the local computer. |
Exists(String, String) |
Determines whether the category is registered on the specified computer. |
Exists(String)
Determines whether the category is registered on the local computer.
public:
static bool Exists(System::String ^ categoryName);
public static bool Exists (string categoryName);
static member Exists : string -> bool
Public Shared Function Exists (categoryName As String) As Boolean
Parameters
- categoryName
- String
The name of the performance counter category to look for.
Returns
true
if the category is registered; otherwise, false
.
Exceptions
The categoryName
parameter is null
.
The categoryName
parameter is an empty string ("").
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 determines whether a PerformanceCounterCategory object named "orders" exists. If not, it creates the PerformanceCounterCategory object by using a CounterCreationDataCollection object that contains two performance counters.
if ( !PerformanceCounterCategory::Exists( "Orders" ) )
{
CounterCreationData^ milk = gcnew CounterCreationData;
milk->CounterName = "milk";
milk->CounterType = PerformanceCounterType::NumberOfItems32;
CounterCreationData^ milkPerSecond = gcnew CounterCreationData;
milkPerSecond->CounterName = "milk orders/second";
milkPerSecond->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;
CounterCreationDataCollection^ ccds = gcnew CounterCreationDataCollection;
ccds->Add( milkPerSecond );
ccds->Add( milk );
PerformanceCounterCategory::Create( "Orders", "Number of processed orders", ccds );
}
if (!PerformanceCounterCategory.Exists("Orders"))
{
CounterCreationData milk = new CounterCreationData();
milk.CounterName = "milk";
milk.CounterType = PerformanceCounterType.NumberOfItems32;
CounterCreationData milkPerSecond = new CounterCreationData();
milkPerSecond.CounterName = "milk orders/second";
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
CounterCreationDataCollection ccds = new CounterCreationDataCollection();
ccds.Add(milkPerSecond);
ccds.Add(milk);
PerformanceCounterCategory.Create("Orders", "Number of processed orders",
PerformanceCounterCategoryType.SingleInstance, ccds);
}
If Not PerformanceCounterCategory.Exists("Orders") Then
Dim milk As New CounterCreationData()
milk.CounterName = "milk"
milk.CounterType = PerformanceCounterType.NumberOfItems32
Dim milkPerSecond As New CounterCreationData()
milkPerSecond.CounterName = "milk orders/second"
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
Dim ccds As New CounterCreationDataCollection()
ccds.Add(milkPerSecond)
ccds.Add(milk)
PerformanceCounterCategory.Create("Orders", "Number of processed orders", _
PerformanceCounterCategoryType.SingleInstance, ccds)
End If
Remarks
Use of the Exists method can result in a noticeable performance penalty while all performance counters on the machine are checked for availability. If you are only writing to a performance counter, you can avoid the global search for performance counters by creating the performance counter when the application is installed and assuming the category exists when accessing the counter. There is no way to avoid the performance counter search when reading from performance counters.
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
Exists(String, String)
Determines whether the category is registered on the specified computer.
public:
static bool Exists(System::String ^ categoryName, System::String ^ machineName);
public static bool Exists (string categoryName, string machineName);
static member Exists : string * string -> bool
Public Shared Function Exists (categoryName As String, machineName As String) As Boolean
Parameters
- categoryName
- String
The name of the performance counter category to look for.
- machineName
- String
The name of the computer to examine for the category.
Returns
true
if the category is registered; otherwise, false
.
Exceptions
The categoryName
parameter is null
.
The categoryName
parameter is an empty string ("").
-or-
The machineName
parameter is invalid.
A call to an underlying system API failed.
The network path cannot be found.
The caller does not have the required permission.
-or-
Code that is executing without administrative privileges attempted to read a performance counter.
Examples
The following example determines whether a PerformanceCounterCategory object named Orders
exists. If it does not exist, the example creates the PerformanceCounterCategory object by using a CounterCreationDataCollection object that contains two performance counters.
if ( !PerformanceCounterCategory::Exists( "Orders" ) )
{
CounterCreationData^ milk = gcnew CounterCreationData;
milk->CounterName = "milk";
milk->CounterType = PerformanceCounterType::NumberOfItems32;
CounterCreationData^ milkPerSecond = gcnew CounterCreationData;
milkPerSecond->CounterName = "milk orders/second";
milkPerSecond->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;
CounterCreationDataCollection^ ccds = gcnew CounterCreationDataCollection;
ccds->Add( milkPerSecond );
ccds->Add( milk );
PerformanceCounterCategory::Create( "Orders", "Number of processed orders", ccds );
}
if (!PerformanceCounterCategory.Exists("Orders"))
{
CounterCreationData milk = new CounterCreationData();
milk.CounterName = "milk";
milk.CounterType = PerformanceCounterType.NumberOfItems32;
CounterCreationData milkPerSecond = new CounterCreationData();
milkPerSecond.CounterName = "milk orders/second";
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
CounterCreationDataCollection ccds = new CounterCreationDataCollection();
ccds.Add(milkPerSecond);
ccds.Add(milk);
PerformanceCounterCategory.Create("Orders", "Number of processed orders",
PerformanceCounterCategoryType.SingleInstance, ccds);
}
If Not PerformanceCounterCategory.Exists("Orders") Then
Dim milk As New CounterCreationData()
milk.CounterName = "milk"
milk.CounterType = PerformanceCounterType.NumberOfItems32
Dim milkPerSecond As New CounterCreationData()
milkPerSecond.CounterName = "milk orders/second"
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
Dim ccds As New CounterCreationDataCollection()
ccds.Add(milkPerSecond)
ccds.Add(milk)
PerformanceCounterCategory.Create("Orders", "Number of processed orders", _
PerformanceCounterCategoryType.SingleInstance, ccds)
End If
Remarks
Use of the Exists method can result in a noticeable performance penalty while all performance counters on the machine are checked for availability. If you are only writing to a performance counter, you can avoid the global search for performance counters by creating the performance counter when the application is installed and assuming the category exists when accessing the counter. There is no way to avoid the performance counter search when reading from performance counters.
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.