PerformanceCounterCategory.GetCounters メソッド

定義

このパフォーマンス カウンター カテゴリのカウンターの一覧を取得します。

オーバーロード

名前 説明
GetCounters()

インスタンスが 1 つだけ含まれるパフォーマンス カウンター カテゴリ内のカウンターの一覧を取得します。

GetCounters(String)

1 つ以上のインスタンスを含むパフォーマンス カウンター カテゴリ内のカウンターの一覧を取得します。

GetCounters()

インスタンスが 1 つだけ含まれるパフォーマンス カウンター カテゴリ内のカウンターの一覧を取得します。

public:
 cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters();
public System.Diagnostics.PerformanceCounter[] GetCounters();
member this.GetCounters : unit -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters () As PerformanceCounter()

返品

この単一インスタンス パフォーマンス カウンター カテゴリに関連付けられているカウンターを示す PerformanceCounter オブジェクトの配列。

例外

カテゴリが 1 つのインスタンスではありません。

基になるシステム API の呼び出しに失敗しました。

カテゴリに関連付けられたインスタンスがありません。

管理特権なしで実行されているコードが、パフォーマンス カウンターを読み取ろうとしました。

次のコード例では、PerformanceCounterCategory内のPerformanceCounter オブジェクトの一覧を取得します。 最初に、コンピューター名が指定されたかどうかに基づいて、適切なコンストラクターで PerformanceCounterCategory を作成します。 その後、 GetCounters メソッドを使用して PerformanceCounter オブジェクトの配列を返し、インスタンス名が指定されたかどうかに基づいて GetCounters オーバーロードを選択します。

この GetCounters() オーバーロードは、単一インスタンス カテゴリで使用されない限り失敗します。

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

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        machineName = args[1]=="."? "": args[1];
        instanceName = args[2];
    }
    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 counters for this instance or a single instance
        // of the selected category.
        if (instanceName.Length>0)
        {
            counters = pcc.GetCounters(instanceName);
        }
        else
        {
            counters = pcc.GetCounters();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get counter information for " +
            (instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
            "category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
            categoryName, machineName, instanceName);
        Console.WriteLine(ex.Message);
        return;
    }

    // Display the counter names if GetCounters was successful.
    if (counters!=null)
    {
        Console.WriteLine("These counters exist in " +
            (instanceName.Length>0? "instance \"{1}\" of": "single instance") +
            " category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
            categoryName, instanceName, machineName);

        // Display a numbered list of the counter names.
        int objX;
        for(objX=0; objX<counters.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim instanceName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim counters() As PerformanceCounter

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        machineName = IIf(args(1) = ".", "", args(1))
        instanceName = args(2)
    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 counters for this instance or a single instance 
        ' of the selected category.
        If instanceName.Length > 0 Then
            counters = pcc.GetCounters(instanceName)
        Else
            counters = pcc.GetCounters()
        End If

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

    ' Display the counter names if GetCounters was successful.
    If Not counters Is Nothing Then
        Console.WriteLine("These counters exist in " & _
            IIf(instanceName.Length > 0, "instance ""{1}"" of", _
            "single instance") & " category {0} on " & _
            IIf(machineName.Length > 0, _
                "computer ""{2}"":", "this computer:"), _
            categoryName, instanceName, machineName)

        ' Display a numbered list of the counter names.
        Dim objX As Integer
        For objX = 0 To counters.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, _
                counters(objX).CounterName)
        Next objX
    End If
End Sub

注釈

パフォーマンス オブジェクト インスタンスの詳細については、 PerformanceCounter クラスの概要を参照してください。

Note

Windows Vista 以降、Windows XP Professional x64 Edition、または Windows Server 2003 の非対話型ログオン セッションからパフォーマンス カウンターを読み取る場合は、パフォーマンス モニター ユーザー グループのメンバーであるか、管理者特権を持っている必要があります。

Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格する必要を回避するには、パフォーマンス モニター ユーザー グループに自分自身を追加します。

Windows Vista 以降では、ユーザー アカウント制御 (UAC) によってユーザーの権限が決定されます。 組み込みの Administrators グループのメンバーである場合は、標準ユーザー アクセス トークンと管理者アクセス トークンという 2 つのランタイム アクセス トークンが割り当てられます。 既定では、標準のユーザー ロールになります。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 これを行うには、アプリケーション アイコンを右クリックし、管理者として実行することを示すことで、アプリケーションを起動します。

こちらもご覧ください

適用対象

GetCounters(String)

1 つ以上のインスタンスを含むパフォーマンス カウンター カテゴリ内のカウンターの一覧を取得します。

public:
 cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters(System::String ^ instanceName);
public System.Diagnostics.PerformanceCounter[] GetCounters(string instanceName);
member this.GetCounters : string -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters (instanceName As String) As PerformanceCounter()

パラメーター

instanceName
String

関連付けられているカウンターの一覧を取得する対象のパフォーマンス オブジェクト インスタンス。

返品

このパフォーマンス カウンター カテゴリの指定したオブジェクト インスタンスに関連付けられているカウンターを示す PerformanceCounter オブジェクトの配列。

例外

instanceName パラメーターはnull

このPerformanceCounterCategory インスタンスのCategoryName プロパティが設定されていません。

-または-

カテゴリには、 instanceName パラメーターで指定されたインスタンスが含まれていません。

基になるシステム API の呼び出しに失敗しました。

管理特権なしで実行されているコードが、パフォーマンス カウンターを読み取ろうとしました。

次のコード例では、PerformanceCounterCategory内のPerformanceCounter オブジェクトの一覧を取得します。 最初に、コンピューター名が指定されたかどうかに基づいて、適切なコンストラクターで PerformanceCounterCategory を作成します。 その後、 GetCounters メソッドを使用して PerformanceCounter オブジェクトの配列を返し、インスタンス名が指定されたかどうかに基づいて GetCounters オーバーロードを選択します。

インスタンスを含むカテゴリで使用しない限り、この GetCounters(String) オーバーロードは失敗します。

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

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        machineName = args[1]=="."? "": args[1];
        instanceName = args[2];
    }
    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 counters for this instance or a single instance
        // of the selected category.
        if (instanceName.Length>0)
        {
            counters = pcc.GetCounters(instanceName);
        }
        else
        {
            counters = pcc.GetCounters();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get counter information for " +
            (instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
            "category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
            categoryName, machineName, instanceName);
        Console.WriteLine(ex.Message);
        return;
    }

    // Display the counter names if GetCounters was successful.
    if (counters!=null)
    {
        Console.WriteLine("These counters exist in " +
            (instanceName.Length>0? "instance \"{1}\" of": "single instance") +
            " category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
            categoryName, instanceName, machineName);

        // Display a numbered list of the counter names.
        int objX;
        for(objX=0; objX<counters.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim instanceName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim counters() As PerformanceCounter

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        machineName = IIf(args(1) = ".", "", args(1))
        instanceName = args(2)
    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 counters for this instance or a single instance 
        ' of the selected category.
        If instanceName.Length > 0 Then
            counters = pcc.GetCounters(instanceName)
        Else
            counters = pcc.GetCounters()
        End If

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

    ' Display the counter names if GetCounters was successful.
    If Not counters Is Nothing Then
        Console.WriteLine("These counters exist in " & _
            IIf(instanceName.Length > 0, "instance ""{1}"" of", _
            "single instance") & " category {0} on " & _
            IIf(machineName.Length > 0, _
                "computer ""{2}"":", "this computer:"), _
            categoryName, instanceName, machineName)

        ' Display a numbered list of the counter names.
        Dim objX As Integer
        For objX = 0 To counters.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, _
                counters(objX).CounterName)
        Next objX
    End If
End Sub

注釈

単一インスタンス カテゴリを表すには、 instanceName パラメーターに空の文字列 ("") を渡します。

パフォーマンス オブジェクト インスタンスの詳細については、 PerformanceCounter クラスの概要を参照してください。

Note

Windows Vista 以降、Windows XP Professional x64 Edition、または Windows Server 2003 の非対話型ログオン セッションからパフォーマンス カウンターを読み取る場合は、パフォーマンス モニター ユーザー グループのメンバーであるか、管理者特権を持っている必要があります。

Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格する必要を回避するには、パフォーマンス モニター ユーザー グループに自分自身を追加します。

Windows Vista 以降では、ユーザー アカウント制御 (UAC) によってユーザーの権限が決定されます。 組み込みの Administrators グループのメンバーである場合は、標準ユーザー アクセス トークンと管理者アクセス トークンという 2 つのランタイム アクセス トークンが割り当てられます。 既定では、標準のユーザー ロールになります。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 これを行うには、アプリケーション アイコンを右クリックし、管理者として実行することを示すことで、アプリケーションを起動します。

こちらもご覧ください

適用対象