PerformanceCounterCategory.CounterExists メソッド

定義

指定したカウンターが特定のカテゴリに登録されているかどうかを判断します。

オーバーロード

CounterExists(String)

指定したカウンターがこのカテゴリに登録されているかどうかを判断します。カテゴリは、CategoryName プロパティと MachineName プロパティで指定されます。

CounterExists(String, String)

指定したカウンターが、ローカル コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

CounterExists(String, String, String)

指定したカウンターが、リモート コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

CounterExists(String)

ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs

指定したカウンターがこのカテゴリに登録されているかどうかを判断します。カテゴリは、CategoryName プロパティと MachineName プロパティで指定されます。

public:
 bool CounterExists(System::String ^ counterName);
public bool CounterExists (string counterName);
member this.CounterExists : string -> bool
Public Function CounterExists (counterName As String) As Boolean

パラメーター

counterName
String

検索するパフォーマンス カウンターの名前。

戻り値

カウンターが、CategoryName プロパティと MachineName プロパティで指定されたカテゴリに登録されている場合は true。それ以外の場合は false

例外

counterNamenull です。

CategoryName プロパティが設定されていません。

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

管理特権を使用せずに実行されているコードがパフォーマンス カウンターの読み取りを試みました。

が存在するかどうかを判断するコード例を次に PerformanceCounter 示します。 指定された場合は、コマンド ラインからカテゴリ名、カウンター名、およびコンピューター名を取得します。 適切な PerformanceCounterCategoryPerformanceCounterCategory使用して オブジェクトを作成します。 次に、 メソッドを CounterExists(String) 使用して、指定した PerformanceCounter が存在するかどうかを判断し、ユーザーに通知します。

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);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False
    Dim pcc As PerformanceCounterCategory

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

    Try
        If machineName.Length = 0 Then
            pcc = New PerformanceCounterCategory(categoryName)
        Else
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        End If

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

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, pcc.CategoryName, pcc.MachineName)
End Sub

注釈

このメソッドを呼び出す前に、 プロパティを CategoryName 設定する必要があります。 それ以外の場合は、例外がスローされます。

プロパティを MachineName 設定していない場合、このメソッドはローカル コンピューター (".") を使用します。

Note

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

Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格させる必要がないようにするには、パフォーマンス モニター Users グループに自分自身を追加します。

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

こちらもご覧ください

適用対象

CounterExists(String, String)

ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs

指定したカウンターが、ローカル コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

public:
 static bool CounterExists(System::String ^ counterName, System::String ^ categoryName);
public static bool CounterExists (string counterName, string categoryName);
static member CounterExists : string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String) As Boolean

パラメーター

counterName
String

検索するパフォーマンス カウンターの名前。

categoryName
String

指定したパフォーマンス カウンターが関連付けられているパフォーマンス カウンター カテゴリ (パフォーマンス オブジェクト) の名前。

戻り値

カウンターがローカル コンピューター上の指定したカテゴリに登録されている場合は true。それ以外の場合は false

例外

categoryNamenull です。

- または -

counterNamenull です。

categoryName が空の文字列 ("") です。

カテゴリ名が存在しません。

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

管理特権を使用せずに実行されているコードがパフォーマンス カウンターの読み取りを試みました。

が存在するかどうかを判断するコード例を次に PerformanceCounter 示します。 指定された場合は、コマンド ラインからカテゴリ名、カウンター名、およびコンピューター名を取得します。 メソッドの静的オーバーロードを使用して、 CounterExists 指定した PerformanceCounter 名前が に存在するかどうかを判断します PerformanceCounterCategory。 オーバーロードは、コンピューター名が指定されているかどうかに基づいて選択されます。

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

    // 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
    {
        // Check whether the specified counter exists.
        // Use the static forms of the CounterExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
        }
    }
    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, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False

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

    Try
        ' Check whether the specified counter exists.
        ' Use the static forms of the CounterExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, categoryName, machineName)
End Sub

注釈

Note

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

Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格させる必要がないようにするには、パフォーマンス モニター Users グループに自分自身を追加します。

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

こちらもご覧ください

適用対象

CounterExists(String, String, String)

ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs
ソース:
PerformanceCounterCategory.cs

指定したカウンターが、リモート コンピューター上の指定したカテゴリに登録されているかどうかを判断します。

public:
 static bool CounterExists(System::String ^ counterName, System::String ^ categoryName, System::String ^ machineName);
public static bool CounterExists (string counterName, string categoryName, string machineName);
static member CounterExists : string * string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String, machineName As String) As Boolean

パラメーター

counterName
String

検索するパフォーマンス カウンターの名前。

categoryName
String

指定したパフォーマンス カウンターが関連付けられているパフォーマンス カウンター カテゴリ (パフォーマンス オブジェクト) の名前。

machineName
String

パフォーマンス カウンター カテゴリと、それに関連付けられているカウンターが存在するコンピューターの名前。

戻り値

カウンターが指定したコンピューター上の指定したカテゴリに登録されている場合は true。それ以外の場合は false

例外

categoryNamenull です。

- または -

counterNamenull です。

categoryName が空の文字列 ("") です。

- または -

machineName が無効です。

カテゴリ名が存在しません。

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

管理特権を使用せずに実行されているコードがパフォーマンス カウンターの読み取りを試みました。

が存在するかどうかを判断するコード例を次に PerformanceCounter 示します。 指定された場合は、コマンド ラインからカテゴリ名、カウンター名、およびコンピューター名を取得します。 メソッドの静的オーバーロードを使用して、 CounterExists 指定した PerformanceCounter 名前が に存在するかどうかを判断します PerformanceCounterCategory。 オーバーロードは、コンピューター名が指定されているかどうかに基づいて選択されます。

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

    // 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
    {
        // Check whether the specified counter exists.
        // Use the static forms of the CounterExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
        }
    }
    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, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False

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

    Try
        ' Check whether the specified counter exists.
        ' Use the static forms of the CounterExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, categoryName, machineName)
End Sub

注釈

Note

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

Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格させる必要がないようにするには、パフォーマンス モニター Users グループに自分自身を追加します。

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

こちらもご覧ください

適用対象