PerformanceCounterCategory.Exists 方法
定义
确定是否在系统上注册了该类别。Determines whether the category is registered on the system.
重载
| Exists(String) |
确定是否在本地计算机上注册了该类别。Determines whether the category is registered on the local computer. |
| Exists(String, String) |
确定是否在指定的计算机上注册了该类别。Determines whether the category is registered on the specified computer. |
Exists(String)
确定是否在本地计算机上注册了该类别。Determines whether the category is registered on the local computer.
public:
static bool Exists(System::String ^ categoryName);
public static bool Exists (string categoryName);
static member Exists : string -> bool
Public Shared Function Exists (categoryName As String) As Boolean
参数
- categoryName
- String
要查找的性能计数器类别的名称。The name of the performance counter category to look for.
返回
如果已注册该类别,则为 true;否则为 false。true if the category is registered; otherwise, false.
例外
categoryName 参数为 null。The categoryName parameter is null.
categoryName 参数为空字符串 ("")。The categoryName parameter is an empty string ("").
对基础系统 API 的调用失败。A call to an underlying system API failed.
以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.
示例
下面的代码示例确定是否 PerformanceCounterCategory 存在名为 "orders" 的对象。The following code example determines whether a PerformanceCounterCategory object named "orders" exists. 如果不是,则它 PerformanceCounterCategory 通过使用 CounterCreationDataCollection 包含两个性能计数器的对象来创建对象。If not, it creates the PerformanceCounterCategory object by using a CounterCreationDataCollection object that contains two performance counters.
if ( !PerformanceCounterCategory::Exists( "Orders" ) )
{
CounterCreationData^ milk = gcnew CounterCreationData;
milk->CounterName = "milk";
milk->CounterType = PerformanceCounterType::NumberOfItems32;
CounterCreationData^ milkPerSecond = gcnew CounterCreationData;
milkPerSecond->CounterName = "milk orders/second";
milkPerSecond->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;
CounterCreationDataCollection^ ccds = gcnew CounterCreationDataCollection;
ccds->Add( milkPerSecond );
ccds->Add( milk );
PerformanceCounterCategory::Create( "Orders", "Number of processed orders", ccds );
}
if (!PerformanceCounterCategory.Exists("Orders"))
{
CounterCreationData milk = new CounterCreationData();
milk.CounterName = "milk";
milk.CounterType = PerformanceCounterType.NumberOfItems32;
CounterCreationData milkPerSecond = new CounterCreationData();
milkPerSecond.CounterName = "milk orders/second";
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
CounterCreationDataCollection ccds = new CounterCreationDataCollection();
ccds.Add(milkPerSecond);
ccds.Add(milk);
PerformanceCounterCategory.Create("Orders", "Number of processed orders",
PerformanceCounterCategoryType.SingleInstance, ccds);
}
If Not PerformanceCounterCategory.Exists("Orders") Then
Dim milk As New CounterCreationData()
milk.CounterName = "milk"
milk.CounterType = PerformanceCounterType.NumberOfItems32
Dim milkPerSecond As New CounterCreationData()
milkPerSecond.CounterName = "milk orders/second"
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
Dim ccds As New CounterCreationDataCollection()
ccds.Add(milkPerSecond)
ccds.Add(milk)
PerformanceCounterCategory.Create("Orders", "Number of processed orders", _
PerformanceCounterCategoryType.SingleInstance, ccds)
End If
注解
使用 Exists 方法可能会导致性能明显下降,同时会检查计算机上的所有性能计数器的可用性。Use of the Exists method can result in a noticeable performance penalty while all performance counters on the machine are checked for availability. 如果只是写入性能计数器,则可以通过在安装应用程序时创建性能计数器,并假定在访问计数器时存在类别,来避免全局搜索性能计数器。If you are only writing to a performance counter, you can avoid the global search for performance counters by creating the performance counter when the application is installed and assuming the category exists when accessing the counter. 从性能计数器读取时,无法避免性能计数器搜索。There is no way to avoid the performance counter search when reading from performance counters.
备注
若要从 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.
另请参阅
适用于
Exists(String, String)
确定是否在指定的计算机上注册了该类别。Determines whether the category is registered on the specified computer.
public:
static bool Exists(System::String ^ categoryName, System::String ^ machineName);
public static bool Exists (string categoryName, string machineName);
static member Exists : string * string -> bool
Public Shared Function Exists (categoryName As String, machineName As String) As Boolean
参数
- categoryName
- String
要查找的性能计数器类别的名称。The name of the performance counter category to look for.
- machineName
- String
要为该类别检查的计算机的名称。The name of the computer to examine for the category.
返回
如果已注册该类别,则为 true;否则为 false。true if the category is registered; otherwise, false.
例外
categoryName 参数为 null。The categoryName parameter is null.
categoryName 参数为空字符串 ("")。The categoryName parameter is an empty string ("").
- 或 --or-
machineName 参数无效。The machineName parameter is invalid.
对基础系统 API 的调用失败。A call to an underlying system API failed.
无法找到网络路径。The network path cannot be found.
调用方没有所要求的权限。The caller does not have the required permission.
- 或 --or-
以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.
示例
下面的示例确定 PerformanceCounterCategory 名为的对象是否 Orders 存在。The following example determines whether a PerformanceCounterCategory object named Orders exists. 如果该对象不存在,则该示例 PerformanceCounterCategory 通过使用 CounterCreationDataCollection 包含两个性能计数器的对象来创建对象。If it does not exist, the example creates the PerformanceCounterCategory object by using a CounterCreationDataCollection object that contains two performance counters.
if ( !PerformanceCounterCategory::Exists( "Orders" ) )
{
CounterCreationData^ milk = gcnew CounterCreationData;
milk->CounterName = "milk";
milk->CounterType = PerformanceCounterType::NumberOfItems32;
CounterCreationData^ milkPerSecond = gcnew CounterCreationData;
milkPerSecond->CounterName = "milk orders/second";
milkPerSecond->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;
CounterCreationDataCollection^ ccds = gcnew CounterCreationDataCollection;
ccds->Add( milkPerSecond );
ccds->Add( milk );
PerformanceCounterCategory::Create( "Orders", "Number of processed orders", ccds );
}
if (!PerformanceCounterCategory.Exists("Orders"))
{
CounterCreationData milk = new CounterCreationData();
milk.CounterName = "milk";
milk.CounterType = PerformanceCounterType.NumberOfItems32;
CounterCreationData milkPerSecond = new CounterCreationData();
milkPerSecond.CounterName = "milk orders/second";
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
CounterCreationDataCollection ccds = new CounterCreationDataCollection();
ccds.Add(milkPerSecond);
ccds.Add(milk);
PerformanceCounterCategory.Create("Orders", "Number of processed orders",
PerformanceCounterCategoryType.SingleInstance, ccds);
}
If Not PerformanceCounterCategory.Exists("Orders") Then
Dim milk As New CounterCreationData()
milk.CounterName = "milk"
milk.CounterType = PerformanceCounterType.NumberOfItems32
Dim milkPerSecond As New CounterCreationData()
milkPerSecond.CounterName = "milk orders/second"
milkPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
Dim ccds As New CounterCreationDataCollection()
ccds.Add(milkPerSecond)
ccds.Add(milk)
PerformanceCounterCategory.Create("Orders", "Number of processed orders", _
PerformanceCounterCategoryType.SingleInstance, ccds)
End If
注解
使用 Exists 方法可能会导致性能明显下降,同时会检查计算机上的所有性能计数器的可用性。Use of the Exists method can result in a noticeable performance penalty while all performance counters on the machine are checked for availability. 如果只是写入性能计数器,则可以通过在安装应用程序时创建性能计数器,并假定在访问计数器时存在类别,来避免全局搜索性能计数器。If you are only writing to a performance counter, you can avoid the global search for performance counters by creating the performance counter when the application is installed and assuming the category exists when accessing the counter. 从性能计数器读取时,无法避免性能计数器搜索。There is no way to avoid the performance counter search when reading from performance counters.
备注
若要从 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.