PerformanceCounterCategory.CounterExists 方法

定义

确定是否向特定的类别注册了指定的计数器。

重载

CounterExists(String)

确定是否向此类别注册了指定的计数器,这由 CategoryNameMachineName 属性指示。

CounterExists(String, String)

确定是否向本地计算机上的指定类别注册了指定的计数器。

CounterExists(String, String, String)

确定是否向远程计算机上的指定类别注册了指定的计数器。

CounterExists(String)

确定是否向此类别注册了指定的计数器,这由 CategoryNameMachineName 属性指示。

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

要查找的性能计数器的名称。

返回

Boolean

如果向 CategoryNameMachineName 属性指定的类别注册了该计数器,则为 true;否则为 false

例外

counterNamenull

尚未设置 CategoryName 属性。

对基础系统 API 的调用失败。

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。

示例

下面的代码示例确定是否存在 PerformanceCounter 。 如果给定类别名称、计数器名称和计算机名称,则从命令行获取它们。 它使用相应的PerformanceCounterCategory对象创建对象PerformanceCounterCategory。 然后,它使用该方法 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 此属性,此方法将使用本地计算机 (“。”) 。

备注

若要从 Windows Vista 及更高版本中的非交互式登录会话读取性能计数器,Windows XP Professional x64 Edition 或 Windows Server 2003,你必须是性能监视器用户组的成员或具有管理权限。

若要避免提升特权以访问 Windows Vista 及更高版本中的性能计数器,请将自己添加到性能监视器用户组。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于

CounterExists(String, String)

确定是否向本地计算机上的指定类别注册了指定的计数器。

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

性能计数器类别的名称或性能对象,指定的性能计数器与之关联。

返回

Boolean

如果向本地计算机上的指定类别注册了该计数器,则为 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

注解

备注

若要从 Windows Vista 及更高版本中的非交互式登录会话读取性能计数器,Windows XP Professional x64 Edition 或 Windows Server 2003,你必须是性能监视器用户组的成员或具有管理权限。

若要避免提升特权以访问 Windows Vista 及更高版本中的性能计数器,请将自己添加到性能监视器用户组。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于

CounterExists(String, String, String)

确定是否向远程计算机上的指定类别注册了指定的计数器。

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

性能计数器类别及其关联计数器所在的计算机的名称。

返回

Boolean

如果向指定计算机上的指定类别注册了该计数器,则为 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

注解

备注

若要从 Windows Vista 及更高版本中的非交互式登录会话读取性能计数器,Windows XP Professional x64 Edition 或 Windows Server 2003,你必须是性能监视器用户组的成员或具有管理权限。

若要避免提升特权以访问 Windows Vista 及更高版本中的性能计数器,请将自己添加到性能监视器用户组。

在 Windows Vista 或更高版本中,用户帐户控制 (UAC) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将特权从标准用户提升到管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于