“System.ComponentModel.Win32Exception:访问被拒绝”调用 PerformanceCounterCategory.Create() 时抛出
在我的计算机上调用下面的 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);
}
Calling PerformanceCounterCategory.Create() below on my machine thorws out this exception:
System.ComponentModel.Win32Exception: Access is denied
And the message reported in Event Viewer goes as following:
The SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib key could not be opened or accessed in order to install counter strings.The Win32 status returned by the call is the first DWORD in Data section.
Do you know what's the issue about it?
Thank you!
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了创建性能计数器,您需要有足够的权限。尝试在管理员帐户下运行此代码。
另一个重要的说明。以下是文档的引用:
In order to create performance counters you need to have sufficient privileges. Try running this code under the administrator account.
Another important note. Here's a quote from the documentation: