This documentation is archived and is not being maintained.

PerformanceCounterCategory.CounterExists Method (String, String, String)

Determines whether the specified counter is registered to the specified category on a remote computer.

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

public static bool CounterExists(
	string counterName,
	string categoryName,
	string machineName


Type: System.String
The name of the performance counter to look for.
Type: System.String
The name of the performance counter category, or performance object, with which the specified performance counter is associated.
Type: System.String
The name of the computer on which the performance counter category and its associated counters exist.

Return Value

Type: System.Boolean
true, if the counter is registered to the specified category on the specified computer; otherwise, false.


The categoryName is null.


The counterName is null.


The categoryName is an empty string ("").


The machineName is invalid.


The category name does not exist.


A call to an underlying system API failed.


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


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 code example determines whether a PerformanceCounter exists. It gets a category name, counter name, and computer name from the command line, if they are given. It uses the static overloads of the CounterExists method to determine whether the specified PerformanceCounter name exists in the PerformanceCounterCategory. The overload is selected based on whether a computer name is provided.

public static void Main(string[] args)
    string categoryName = "";
    string counterName = "";
    string machineName = "";
    bool objectExists = false;

    // Copy the supplied arguments into the local variables.
        categoryName = args[0];
        counterName = args[1];
        machineName = args[2]=="."? "": args[2];
    catch(Exception ex)
        // Ignore the exception from non-supplied arguments.

        // Check whether the specified counter exists.
        // Use the static forms of the CounterExists method.
        if (machineName.Length==0)
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);

    catch(Exception ex)
        Console.WriteLine("Unable to check for the existence of " +
            "counter \"{0}\" in category \"{1}\" on " + 
            (machineName.Length>0? "computer \"{2}\".": "this computer.") + "\n" + 
            ex.Message, counterName, categoryName, machineName);

    // Tell the user whether the counter exists.
    Console.WriteLine("Counter \"{0}\" "+ (objectExists? "exists": "does not exist") + 
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."), 
        counterName, categoryName, machineName);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.