Export (0) Print
Expand All

PerformanceCounterCategory.GetInstanceNames Method

Retrieves the list of performance object instances that are associated with this category.

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

public string[] GetInstanceNames ()
public String[] GetInstanceNames ()
public function GetInstanceNames () : String[]
Not applicable.

Return Value

An array of strings representing the performance object instance names that are associated with this category or, if the category contains only one performance object instance, a single-entry array that contains an empty string ("").

Exception typeCondition

InvalidOperationException

The CategoryName property is a null reference (Nothing in Visual Basic). The property might not have been set.

-or-

The category does not have an associated instance.

Win32Exception

A call to an underlying system API failed.

UnauthorizedAccessException

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

NoteNote:

To read performance counters in Windows Vista, 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, add yourself to the Performance Monitor Users group.

In Windows Vista, 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 gets a list of the PerformanceCounter objects in a PerformanceCounterCategory. It first creates a PerformanceCounterCategory object, using the appropriate constructor based on whether a computer name was specified. It then uses GetInstanceNames to return the instance names as an array of String, which it sorts and displays.

public static void Main(string[] args)
{
    string categoryName = "";
    string machineName = "";
    PerformanceCounterCategory pcc;
    string[] instances;

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

    try
    {
        // Create the appropriate PerformanceCounterCategory object.
        if (machineName.Length>0)
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }

        // Get the instances associated with this category.
        instances = pcc.GetInstanceNames();

    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get instance information for " +
            "category \"{0}\" on " + 
            (machineName.Length>0? "computer \"{1}\":": "this computer:"), 
            categoryName, machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    //If an empty array is returned, the category has a single instance.
    if (instances.Length==0)
    {
        Console.WriteLine("Category \"{0}\" on " + 
            (machineName.Length>0? "computer \"{1}\"": "this computer") + 
            " is single-instance.", pcc.CategoryName, pcc.MachineName);
    }
    else
    {
        // Otherwise, display the instances.
        Console.WriteLine("These instances exist in category \"{0}\" on " + 
            (machineName.Length>0? "computer \"{1}\".": "this computer:"), 
            pcc.CategoryName, pcc.MachineName);

        Array.Sort(instances);
        int objX;
        for(objX=0; objX<instances.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, instances[objX]);
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft