PerformanceCounters在.NET 4.0和放大器; Windows 7的放大器、NET、PerformanceCounters、Windows

2023-09-07 11:04:05 作者:余生无你

我有一个程序,在VS2008和Vista工作得很好,但我对Windows 7和VS2010 / .NET Framework 4.0中尝试它,它不工作。最终的问题是,System.Diagnostics.PerformanceCounterCategory.GetCategories() (和其他PerformanceCounterCategory方法)无法正常工作。我得到一个System.InvalidOperationException消息无法加载计数器名称数据,因为无效的指数'是从注册表中读取。

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);
        }
    }
}

我没有确保我运行的程序作为管理员。没关系,如果我与VS /调试器附加或不运行它。我没有另一台机器在Windows 7或VS2010来测试它,所以我不知道这是在这里复杂的事情(或者两者兼而有之?)。它是Windows 7 x64和我试着迫使应用程序在两个x32和x64的运行,但得到相同的结果。

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.

推荐答案

看来性能计数器已损坏我的系统上。虽然我没有按照这个帖子确切地说,它使我的解。这是我做的:

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...

然后输入:

lodctr /R

据从先行一步的文档,这得到窗口的重建PERF注册表字符串和基于当前的注册表设置和备份INI文件从头信息。的我有一种感觉,这是什么做了魔术。然而,接下来我注意到了.NET性能计数器不存在了,所以基于这个我输入以下内容重新加载它们:

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"

请注意,这个路径是用于.NET Framework 4.0的64位。你可以想像的框架/平台的其他变化的路径。我的猜测的你应该总是加载从最高版本,您已安装了.NET框架的柜台,但是这只是一个猜测。

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!