问题描述
我有一个在 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/Debugger 的情况下运行它并不重要.我没有另一台装有 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 /?
里面有用的东西……
然后输入:
lodctr /R
根据上一步中的文档,这会让 Windows 根据当前的注册表设置和备份 INI 文件从头开始重建 perf 注册表字符串和信息. 我感觉这就是做了魔术.但是,接下来我注意到 .NET 性能计数器不再存在,因此基于 this 我输入了以下内容来重新加载它们:
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:WindowsMicrosoft.NETFramework64v4.0.20506corperfmonsymbols.ini"
请注意,此路径适用于 x64 上的 .NET Framework 4.0.您可以想象框架/平台的其他变体的路径.我猜测您应该始终从已安装的 .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!
这篇关于.NET 4.0 & 上的性能计数器Windows 7的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!