PerformanceCounterCategory.InstanceExists Method

Definition

Determines whether the category contains the specified performance object instance.

Overloads

InstanceExists(String)

Determines whether the specified performance object instance exists in the category that is identified by this PerformanceCounterCategory object's CategoryName property.

InstanceExists(String, String)

Determines whether a specified category on the local computer contains the specified performance object instance.

InstanceExists(String, String, String)

Determines whether a specified category on a specified computer contains the specified performance object instance.

InstanceExists(String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determines whether the specified performance object instance exists in the category that is identified by this PerformanceCounterCategory object's CategoryName property.

C#
public bool InstanceExists(string instanceName);

Parameters

instanceName
String

The performance object instance in this performance counter category to search for.

Returns

true if the category contains the specified performance object instance; otherwise, false.

Exceptions

The CategoryName property is null. The property might not have been set.

The instanceName parameter is null.

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 PerformanceCounter instance exists within a PerformanceCounterCategory. It first creates a PerformanceCounterCategory object, using the appropriate constructor based on whether a computer name was specified. It then uses InstanceExists(String) to determine whether the specified instance exists, then informs the user. If no instance name is specified, the example uses the default single-instance name.

C#
public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    PerformanceCounterCategory pcc;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

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

        // Check whether the instance exists.
        // Use the per-instance overload of InstanceExists.
        objectExists = pcc.InstanceExists(instanceName);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") +
            "\n" + ex.Message, instanceName, categoryName, machineName);
        return;
    }

    // Tell the user whether the instance exists.
    Console.WriteLine("Instance \"{0}\" " + (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        instanceName, pcc.CategoryName, pcc.MachineName);
}

Remarks

This overload of InstanceExists is not static. It requires you to create a PerformanceCounterCategory object and to set the CategoryName property.

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 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

InstanceExists(String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determines whether a specified category on the local computer contains the specified performance object instance.

C#
public static bool InstanceExists(string instanceName, string categoryName);

Parameters

instanceName
String

The performance object instance to search for.

categoryName
String

The performance counter category to search.

Returns

true if the category contains the specified performance object instance; otherwise, false.

Exceptions

The instanceName parameter is null.

-or-

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 uses the static overloads of InstanceExists to determine whether the given PerformanceCounter instance exists in the PerformanceCounterCategory. The overload is selected based on whether a computer name is specified. If no instance name is specified, the example uses the default single-instance name.

C#
public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

    try
    {
        // Check whether the specified instance exists.
        // Use the static forms of the InstanceExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") + "\n" +
            ex.Message, instanceName, categoryName, machineName);
        return;
    }

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

Remarks

It is not possible to determine whether a performance object instance exists on a computer without specifying a specific category to look in.

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 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

InstanceExists(String, String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determines whether a specified category on a specified computer contains the specified performance object instance.

C#
public static bool InstanceExists(string instanceName, string categoryName, string machineName);

Parameters

instanceName
String

The performance object instance to search for.

categoryName
String

The performance counter category to search.

machineName
String

The name of the computer on which to look for the category instance pair.

Returns

true if the category contains the specified performance object instance; otherwise, false.

Exceptions

The instanceName parameter is null.

-or-

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.

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

Examples

The following code example uses the static overloads of InstanceExists to determine whether the given PerformanceCounter instance exists in the PerformanceCounterCategory. The overload is selected based on whether a computer name is specified. If no instance name is specified, the example uses the default single-instance name.

C#
public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

    try
    {
        // Check whether the specified instance exists.
        // Use the static forms of the InstanceExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "instance \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\":": "this computer:") + "\n" +
            ex.Message, instanceName, categoryName, machineName);
        return;
    }

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

Remarks

It is not possible to determine whether a performance object instance exists on a computer without specifying a specific category to look in.

You can use "." to specify the local computer.

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 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10