views:

461

answers:

1

Hi guys:

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);
        }
+1  A: 

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.

Darin Dimitrov
as as i remember you need even admin rights
Andrey
I shall have admin privilege. However, I am authorized by AD.
Ricky
You shall not with UAC enabled.
Hans Passant