PerformanceCounterCategory.GetCategories() in 1.1

本文关键字:in GetCategories PerformanceCounterCategory | 更新日期: 2023-09-27 18:08:08

我想在PerformanceCounterCategory.GetCategories()中添加CategoryNAme ="Processor Information"属性在1.1框架中有79个类别,但在2.0中有141个类别我的问题是我可以在1.1框架中获取"Processor Information"属性还是我可以手动添加该信息?

PerformanceCounterCategory.GetCategories() in 1.1

如果在您的 performance .exe中没有Processor Information性能计数器类别(一般情况下,这是不可能的),您可以将该类别添加为PerformanceCounterCategoryCounterCreationDataCollection类的手动使用。让我们用Processor Information类别创建% Processor Time性能计数器。例如,

if (!PerformanceCounterCategory.Exists("Processor Information"))
{
    CounterCreationDataCollection counter = new CounterCreationDataCollection();
    CounterCreationData data = new CounterCreationData();
    data.CounterName = "% Processor Time";
    totalOps.CounterHelp = "The percentage of time the processor was busy during the sampling interval";
    counter.Add(data);
    PerformanceCounterCategory.Create("Processor Information",
                                      "This is a sample", 
                                      PerformanceCounterCategoryType.MultiInstance, 
                                      counter);
}

顺便说一下;

在Windows Vista和更高版本,Windows XP中读取性能计数器Professional x64 Edition或Windows Server 2003,您必须是性能监视器用户组的成员,或者具有管理权限特权。

避免必须提升权限才能访问性能在Windows Vista和更高版本的计数器中,将自己添加到性能

在Windows Vista及更新版本中,用户帐户控制(UAC)决定用户的权限。如果您是内置的成员管理员组中,将为您分配两个运行时访问令牌标准用户访问令牌和管理员访问令牌。通过默认情况下,您处于标准用户角色中。来执行代码访问性能计数器时,必须首先提升权限从标准用户到管理员。你可以在开始的时候这样做通过右键单击应用程序图标并指出您要以管理员身份运行。