在我的机器上调用 PerformanceCounterCategory.Create() 可以消除这个异常:
System.ComponentModel.Win32Exception:访问被拒绝
事件查看器中报告的消息如下:
无法打开或访问 SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib 键以安装计数器字符串。调用返回的 Win32 状态是数据部分中的第一个 DWORD。
你知道它有什么问题吗?
谢谢!
if (!PerformanceCounterCategory.Exists("MyCategory"))
{
CounterCreationDataCollection counters = new CounterCreationDataCollection();
CounterCreationData avgDurationBase = new CounterCreationData();
avgDurationBase.CounterName = "average time per operation base";
avgDurationBase.CounterHelp = "Average duration per operation execution base";
avgDurationBase.CounterType = PerformanceCounterType.AverageBase;
counters.Add(avgDurationBase);
// create new category with the counters above
PerformanceCounterCategory.Create("MyCategory",
"Sample category for Codeproject", PerformanceCounterCategoryType.SingleInstance, counters);
}