PerformanceCounterCategory.GetInstanceNames PerformanceCounterCategory.GetInstanceNames PerformanceCounterCategory.GetInstanceNames PerformanceCounterCategory.GetInstanceNames Method

定義

このカテゴリに関連付けられたパフォーマンス オブジェクト インスタンスの一覧を取得します。Retrieves the list of performance object instances that are associated with this category.

public:
 cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames ();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()

戻り値

String[]

このカテゴリに関連付けられたパフォーマンス オブジェクト インスタンス名を表す文字列の配列。または、カテゴリに含まれるパフォーマンス オブジェクト インスタンスが 1 つだけの場合は、空の文字列 ("") が格納された単一エントリの配列。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 ("").

例外

CategoryName プロパティが null である。The CategoryName property is null. プロパティが設定されていない可能性があります。The property might not have been set.

- または --or- カテゴリには関連付けられたインスタンスはありません。The category does not have an associated instance.

基になるシステム API の呼び出しに失敗しました。A call to an underlying system API failed.

管理特権を使用せずに実行されているコードがパフォーマンス カウンターの読み取りを試みました。Code that is executing without administrative privileges attempted to read a performance counter.

次のコード例PerformanceCounter PerformanceCounterCategoryでは、内のオブジェクトの一覧を取得します。The following code example gets a list of the PerformanceCounter objects in a PerformanceCounterCategory. まず、コンピューター名PerformanceCounterCategoryが指定されたかどうかに基づいて、適切なコンストラクターを使用してオブジェクトを作成します。It first creates a PerformanceCounterCategory object, using the appropriate constructor based on whether a computer name was specified. 次に、 GetInstanceNamesを使用して、インスタンス名をのString配列として返します。これにより、が並べ替えられて表示されます。It then uses GetInstanceNames to return the instance names as an array of String, which it sorts and displays.

public:
    static void Main(array<String^>^ args)
    {
        String^ categoryName = "";
        String^ machineName = "";
        PerformanceCounterCategory^ pcc;
        array<String^>^ instances;

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

        try
        {
            // Create the appropriate PerformanceCounterCategory object.
            if (machineName->Length > 0)
            {
                pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
            }
            else
            {
                pcc = gcnew 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]);
            }
        }
    }
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
    {
        // 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]);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim instances() As String

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        machineName = IIf(args(1) = ".", "", args(1))
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Create the appropriate PerformanceCounterCategory object.
        If machineName.Length > 0 Then
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        Else
            pcc = New PerformanceCounterCategory(categoryName)
        End If

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

    Catch ex As Exception
        Console.WriteLine("Unable to get instance information for " & _
             "category ""{0}"" on " & IIf(machineName.Length > 0, _
             "computer ""{1}"":", "this computer:"), _
             categoryName, machineName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    'If an empty array is returned, the category has a single instance.
    If instances.Length = 0 Then
        Console.WriteLine( _
            "Category ""{0}"" on " & IIf(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 " & _
            IIf(machineName.Length > 0, _
                "computer ""{1}"".", "this computer:"), _
            pcc.CategoryName, pcc.MachineName)

        Array.Sort(instances)
        Dim objX As Integer
        For objX = 0 To instances.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, instances(objX))
        Next objX
    End If
End Sub

注釈

注意

Windows Vista 以降、Windows XP Professional x64 Edition、または Windows Server 2003 でパフォーマンスカウンターを読み取るには、Performance Monitor Users グループのメンバであるか、管理者特権を持っている必要があります。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.

Windows Vista 以降のパフォーマンスカウンターにアクセスするために特権を昇格させる必要がないようにするには、Performance Monitor Users グループに自分自身を追加します。To avoid having to elevate your privileges to access performance counters in Windows Vista and later, add yourself to the Performance Monitor Users group.

Windows Vista 以降では、ユーザー アカウント制御 (UAC: User Account Control) でユーザーの権限が決定されます。In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. ユーザーが組み込みの Administrators グループのメンバーである場合、そのユーザーには標準ユーザー アクセス トークンおよび管理者アクセス トークンの 2 つのランタイム アクセス トークンが割り当てられています。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.

適用対象

こちらもご覧ください