I want to create a situation where .Net throws this exception:
InvalidOperationException: Cannot load Counter Name data because an invalid index '' was read from the registry.
For example, in this question Cannot load Counter Name data because an invalid index -Exception
The reason I want to do this is because a small but significant percentage of the users of my application encounter this and I want to detect that situation and show a dialog that can help users fix it by running lodctr. (Unfortunately, simply fixing the calling code isn't an option as the exception is thrown in a third party library) To validate this process, I will need to be able to encounter the problem.
It should go without saying, but I would like to corrupt the performance counter registry in a way that my computer remains usable and is easily recoverable by using lodctr. I don't want to brick my machine.
I have had to solve the same problem for different reasons. Pablissimo's method, and other like it, never worked for me. Here was my method, which has always left my machine entirely usable.
On the machine where you want to corrupt the counters, issue your recovery command: lodctr /r
or whatever.
Once it begins rebuilding the counters, wait a few seconds, then abruptly kill the power to the machine before it can finish. This is a hard kill. Just, BAM! No power.
Timing is not a huge requirement, but is mildly important. If you're too early, it will fail to corrupt the performance counters. If you're too late, it will successfully complete the rebuild.