PerformanceCounterCategory.Exists Method (String, String)


Determines whether the category is registered on the specified computer.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public static bool Exists(
	string categoryName,
	string machineName


Type: System.String

The name of the performance counter category to look for.

Type: System.String

The name of the computer to examine for the category.

Return Value

Type: System.Boolean

true if the category is registered; otherwise, false.

Exception Condition

The categoryName parameter is null.


The categoryName parameter is an empty string ("").


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.


Code that is executing without administrative privileges attempted to read a performance counter.

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.


To read performance counters 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.

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 = 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();

    PerformanceCounterCategory.Create("Orders", "Number of processed orders",
        PerformanceCounterCategoryType.SingleInstance, ccds);



for reading performance counter categories. Associated enumeration: PerformanceCounterPermissionAccess.Read.

.NET Framework
Available since 1.1
Return to top