PerformanceCounterCategory.CounterExists 方法

定义

确定是否向特定的类别注册了指定的计数器。Determines whether a specified counter is registered to a particular category.

重载

CounterExists(String)

确定是否向此类别注册了指定的计数器,这由 CategoryNameMachineName 属性指示。Determines whether the specified counter is registered to this category, which is indicated by the CategoryName and MachineName properties.

CounterExists(String, String)

确定是否向本地计算机上的指定类别注册了指定的计数器。Determines whether the specified counter is registered to the specified category on the local computer.

CounterExists(String, String, String)

确定是否向远程计算机上的指定类别注册了指定的计数器。Determines whether the specified counter is registered to the specified category on a remote computer.

CounterExists(String)

确定是否向此类别注册了指定的计数器,这由 CategoryNameMachineName 属性指示。Determines whether the specified counter is registered to this category, which is indicated by the CategoryName and MachineName properties.

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

要查找的性能计数器的名称。The name of the performance counter to look for.

返回

Boolean

如果向 CategoryNameMachineName 属性指定的类别注册了该计数器,则为 true;否则为 falsetrue if the counter is registered to the category that is specified by the CategoryName and MachineName properties; otherwise, false.

例外

counterNamenullThe counterName is null.

尚未设置 CategoryName 属性。The CategoryName property has not been set.

对基础系统 API 的调用失败。A call to an underlying system API failed.

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.

示例

下面的代码示例确定是否 PerformanceCounter 存在。The following code example determines whether a PerformanceCounter exists. 它从命令行获取类别名称、计数器名称和计算机名称(如果已提供)。It gets a category name, counter name, and computer name from the command line, if they are given. PerformanceCounterCategory 使用适当的创建对象 PerformanceCounterCategoryIt creates a PerformanceCounterCategory object using the appropriate PerformanceCounterCategory. 然后,它使用 CounterExists(String) 方法来确定指定的是否 PerformanceCounter 存在,并通知用户。It then uses the CounterExists(String) method to determine whether the specified PerformanceCounter exists, and informs the user.

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 先设置属性,然后才能调用此方法。You must set the CategoryName property before calling this method. 否则会引发异常。Otherwise, an exception is thrown.

如果尚未设置 MachineName 属性,则此方法使用本地计算机 ( "。) 。If you have not set the MachineName property, this method uses the local computer (".").

备注

若要从 Windows Vista 和更高版本、Windows XP Professional x64 Edition 或 Windows Server 2003 中的非交互式登录会话读取性能计数器,你必须是 "性能监视器用户" 组的成员或具有管理权限。To read performance counters from a non-interactive logon session 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 和更高版本中提升权限来访问性能计数器,请将自己添加到性能监视器用户组。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) 决定用户的特权。In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。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.

另请参阅

适用于

CounterExists(String, String)

确定是否向本地计算机上的指定类别注册了指定的计数器。Determines whether the specified counter is registered to the specified category on the local computer.

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

要查找的性能计数器的名称。The name of the performance counter to look for.

categoryName
String

性能计数器类别的名称或性能对象,指定的性能计数器与之关联。The name of the performance counter category, or performance object, with which the specified performance counter is associated.

返回

Boolean

如果向本地计算机上的指定类别注册了该计数器,则为 true;否则为 falsetrue, if the counter is registered to the specified category on the local computer; otherwise, false.

例外

categoryNamenullThe categoryName is null.

- 或 --or- counterNamenullThe counterName is null.

categoryName 是空字符串 ("")。The categoryName is an empty string ("").

类别名称不存在。The category name does not exist.

对基础系统 API 的调用失败。A call to an underlying system API failed.

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.

示例

下面的代码示例确定是否 PerformanceCounter 存在。The following code example determines whether a PerformanceCounter exists. 它从命令行获取类别名称、计数器名称和计算机名称(如果已提供)。It gets a category name, counter name, and computer name from the command line, if they are given. 它使用方法的静态重载 CounterExists 来确定中是否存在指定的 PerformanceCounter 名称 PerformanceCounterCategoryIt uses the static overloads of the CounterExists method to determine whether the specified PerformanceCounter name exists in the PerformanceCounterCategory. 根据是否提供了计算机名来选择重载。The overload is selected based on whether a computer name is provided.

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 中的非交互式登录会话读取性能计数器,你必须是 "性能监视器用户" 组的成员或具有管理权限。To read performance counters from a non-interactive logon session 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 和更高版本中提升权限来访问性能计数器,请将自己添加到性能监视器用户组。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) 决定用户的特权。In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。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.

另请参阅

适用于

CounterExists(String, String, String)

确定是否向远程计算机上的指定类别注册了指定的计数器。Determines whether the specified counter is registered to the specified category on a remote computer.

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

要查找的性能计数器的名称。The name of the performance counter to look for.

categoryName
String

性能计数器类别的名称或性能对象,指定的性能计数器与之关联。The name of the performance counter category, or performance object, with which the specified performance counter is associated.

machineName
String

性能计数器类别及其关联计数器所在的计算机的名称。The name of the computer on which the performance counter category and its associated counters exist.

返回

Boolean

如果向指定计算机上的指定类别注册了该计数器,则为 true;否则为 falsetrue, if the counter is registered to the specified category on the specified computer; otherwise, false.

例外

categoryNamenullThe categoryName is null.

- 或 --or- counterNamenullThe counterName is null.

categoryName 是空字符串 ("")。The categoryName is an empty string ("").

- 或 --or- machineName 无效。The machineName is invalid.

类别名称不存在。The category name does not exist.

对基础系统 API 的调用失败。A call to an underlying system API failed.

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.

示例

下面的代码示例确定是否 PerformanceCounter 存在。The following code example determines whether a PerformanceCounter exists. 它从命令行获取类别名称、计数器名称和计算机名称(如果已提供)。It gets a category name, counter name, and computer name from the command line, if they are given. 它使用方法的静态重载 CounterExists 来确定中是否存在指定的 PerformanceCounter 名称 PerformanceCounterCategoryIt uses the static overloads of the CounterExists method to determine whether the specified PerformanceCounter name exists in the PerformanceCounterCategory. 根据是否提供了计算机名来选择重载。The overload is selected based on whether a computer name is provided.

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 中的非交互式登录会话读取性能计数器,你必须是 "性能监视器用户" 组的成员或具有管理权限。To read performance counters from a non-interactive logon session 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 和更高版本中提升权限来访问性能计数器,请将自己添加到性能监视器用户组。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) 决定用户的特权。In Windows Vista and later, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。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.

另请参阅

适用于