开发者

"System.ComponentModel.Win32Exception: Access is denied" is thrown when calling PerformanceCounterCategory.Create()

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("M开发者_StackOverflow中文版yCategory",
                "Sample category for Codeproject", PerformanceCounterCategoryType.SingleInstance, counters);
        }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜