“System.ComponentModel.Win32Exception:访问被拒绝”调用 PerformanceCounterCategory.Create() 时抛出

发布于 2024-08-27 02:47:23 字数 1091 浏览 3 评论 0原文

在我的计算机上调用下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-09-03 02:47:23

为了创建性能计数器,您需要有足够的权限。尝试在管理员帐户下运行此代码。

另一个重要的说明。以下是文档的引用:

强烈建议新
性能计数器类别为
在安装期间创建
应用程序,而不是执行期间
的应用程序。这允许时间
用于刷新操作系统
其注册表现清单
计数器类别。如果列表中有
未刷新,尝试使用
该类别将失败。

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:

It is strongly recommended that new
performance counter categories be
created during the installation of the
application, not during the execution
of the application. This allows time
for the operating system to refresh
its list of registered performance
counter categories. If the list has
not been refreshed, the attempt to use
the category will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文