Can't get the CPU Usage in C# using PerformanceCounter - System.FormatException

1.6k views Asked by At

I always used this simple code (from #278071) to get CPU usage:

var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

public string getCurrentCpuUsage() {
        return cpuCounter.NextValue()+"%";
}

It worked perfectly for years. But it's not working after upgrade. I'm using Windows 10 now and cpuCounter.NextValue throws System.FormatException: Input string was not in a correct format.

What's wrong? Is it because of Windows 10? Or it's because of my new CPU - Xeon E5-2683? How can I get the CPU usage?

upd: System.FormatException is thrown when I'm using .NET Framework 3.5. I switched to .NET Framework 4.5 and now it throws InvalidOperationException: Cannot load Counter Name data because an invalid index '' was read from the registry.

1

There are 1 answers

0
Cluster On

Solution: Cannot load Counter Name data because an invalid index -Exception

Just run "lodctr /r" as Administrator. Everything is fine now.