33

I have a program that works fine on VS2008 and Vista, but I'm trying it on Windows 7 and VS2010 / .NET Framework 4.0 and it's not working. Ultimately the problem is that System.Diagnostics.PerformanceCounterCategory.GetCategories() (and other PerformanceCounterCategory methods) is not working. I'm getting a System.InvalidOperationException with the message "Cannot load Counter Name data because an invalid index '' was read from the registry."

I can reproduce this with the very simple program shown below:

class Program
{
    static void Main(string[] args)
    {
        foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())
        {
            Console.WriteLine(pc.CategoryName);
        }
    }
}

I did make sure I'm running the program as an admin. It doesn't matter if I run it with VS/Debugger attached or not. I don't have another machine with Windows 7 or VS2010 to test it on, so I'm not sure which is complicating things here (or both?). It is Windows 7 x64 and I've tried forcing the app to run in both x32 and x64 but get the same results.

Joshua
  • 7,792
  • 3
  • 31
  • 39
Scott Willeke
  • 7,869
  • 1
  • 35
  • 47
  • 1
    If you're not sure about it Windows vs .net version, you could try changing the project properties in VS to target .net 3.5, rebuild, and try again. Or you could build the same code on a vista/xp machine with 2008, and copy the exe to the Win7 machine. – JMarsch Oct 08 '09 at 22:54

1 Answers1

86

It seems performance counters were corrupted on my system. Although I didn't follow this post exactly, it led me to the solution. Here is what I did:

In an command prompt with administrator/elevate privileges typed the following:

lodctr /?

Useful stuff in there...

Then typed:

lodctr /R

According to the docs from the prior step, this gets windows to rebuild the perf registry strings and info from scratch based on the current registry settings and backup INI files. I have a feeling this is what did the magic. However, next I noticed the .NET performance counters were not there anymore so based on this I typed the following to reload them:

lodctr "C:\Windows\Microsoft.NET\Framework64\v4.0.20506\corperfmonsymbols.ini"

Note that this path is for .NET Framework 4.0 on x64. You can imagine the path for other variations of the framework/platform. I'm guessing you should always load the counters from the highest version of the .NET framework that you have installed, but that is just a guess.

I hope this helps someone else someday!

Scott Willeke
  • 7,869
  • 1
  • 35
  • 47
  • 1
    Is there any idea as to what causes this to happen? – JeremyK Mar 05 '12 at 14:17
  • @JeremyK I'm not sure what causes it, but it seems to happen to a lot of people! – Scott Willeke Mar 06 '12 at 02:20
  • 1
    Thanks for the answer, saved a lot of time for me. – JeremyK Mar 06 '12 at 13:03
  • 1
    This was an annoying bug, thanks for the answer, fixed it for me too! – John Warlow Oct 08 '12 at 16:01
  • 1
    This answer just saved me a bunch of time too. Thanks! – KnightsArmy Oct 18 '12 at 20:27
  • 1
    I was about to start re-writing my code to find out what was wrong. Lucky I came across this, everything's working fine for me :) – Sandeep Bansal Dec 27 '12 at 22:26
  • 1
    I do love you, bro! Actually we need to rebuild from the ini file first, then lodctr /R, and it seems it needs to run with administrator access to succeed. – unruledboy Mar 19 '13 at 10:02
  • 1
    This fixed the problem I was having with Raven not working on starting the .exe. Thank you very much Scott. – Subby May 13 '13 at 14:26
  • 5
    I only had to run `lodctr /R` to resolve the same issue for me. – absynce Jun 03 '13 at 20:41
  • This did not seem to fix the issue for me. I updated the path to v4.0.30319, and it on x32 and x64 paths, it said "Counters for .NETFramework are already installed". – Contango Dec 05 '13 at 17:26
  • 1
    The .GetCategories command. The root cause of the issue was nVidia drivers, see http://stackoverflow.com/questions/18751789/performancecountercategory-getcategories-throws-exception-argumentexception – Contango Dec 08 '13 at 13:10
  • This is the path for Windows 8 and Windows Server 2012: lodctr /r %WINDIR%\Inf\.NETFramework\CORPerfMonSymbols.ini – sqeez3r Oct 16 '14 at 07:11