PerformanceCounterCategory.CounterExists Method (String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Determines whether the specified counter is registered to this category, which is indicated by the CategoryName and MachineName properties.

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

public bool CounterExists(
	string counterName
)

Parameters

counterName
Type: System.String

The name of the performance counter to look for.

Return Value

Type: System.Boolean

true if the counter is registered to the category that is specified by the CategoryName and MachineName properties; otherwise, false.

Exception Condition
ArgumentNullException

The counterName is null.

InvalidOperationException

The CategoryName property has not been set.

Win32Exception

A call to an underlying system API failed.

UnauthorizedAccessException

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

You must set the CategoryName property before calling this method. Otherwise, an exception is thrown.

If you have not set the MachineName property, this method uses the local computer (".").

System_CAPS_noteNote

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 creates a PerformanceCounterCategory object using the appropriate PerformanceCounterCategory. It then uses the CounterExists(String) method to determine whether the specified PerformanceCounter exists, and informs the user.

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

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

    try
    {
        if (machineName.Length==0)
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }

        // Check whether the specified counter exists.
        // Use the per-instance overload of CounterExists.
        objectExists = pcc.CounterExists(counterName);

    }
    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);
        return;
    }

    // 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, pcc.CategoryName, pcc.MachineName);
}

.NET Framework
Available since 1.1
Return to top
Show: