Fix Corrupted Performance Counters

As part of the project I am working on, I had to create performance counter categories and counters for monitoring and debugging purposes. So I wrote the code that installs the counters and everything worked as expected on my machines. Then someone told me that the application failed on his machine with this error:

ERROR: caught unexpected exception System.ComponentModel.Win32Exception: The configuration registry key is invalid

   at System.Diagnostics.PerformanceCounterLib.RegisterFiles(String arg0, Boolean unregister)

   at System.Diagnostics.PerformanceCounterLib.UnregisterCategory(String categoryName)

   at System.Diagnostics.PerformanceCounterCategory.Delete(String categoryName)

   at MyProgram.PerfCountersHelper.DeleteCategory(String categoryName)

   at MyProgram.PerfCountersHelper.CleanupCategories()

I looked at the code and everything seemed fine - before installing the new category, I was looking if a category with the same name exists and if it did, I deleted it. After I scratched my head for a while, I decided to take a look at his machine. And surprise, surprise, perfmon showed that there were some default counters missing, which hinted toward perf counter corruption.

Performance counters can become corrupted; so if you don't see categories in Perfmon, or they are missing counters, or names are replaces with numbers, you're probably experiencing this phenomena. To fix the issue, run the lodctr tool with the option /R from a commnd prompt with administrative privileges: LODCTR /R
This command rebuilds the perf registry strings and info from scratch based on the current registry settings and backup INI files.

If this doesn't work or you need more specific steps, look at the article https://support.microsoft.com/?kbid=300956