我试图运行下面的代码:ManagementObjectSearcher get()方法抛出一个异常
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Processor");
// This line throws the exception
ManagementObjectCollection moc = mos.Get();
,我得到了以下错误时抛出:
System.Management.ManagementException: Invalid class
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at LicenseCheckThingy.Form1.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
供参考,该系统我运行在安装了.net 3.5的Windows XP SP 3计算机上。
本机上的用户配置为管理员,howerver未使用“管理员”帐户。
我用basicaly创建了一个示例项目,只是将其中的代码作为错误证明。我知道,通过在两行中添加简单的显示消息,通过堆栈跟踪中的“ManagementObjectEnumerator.MoveNext()”,mos.Get()是引发错误和异常文本似乎支持的行。无论如何,我甚至无法在机器上寻找什么。
请注意,我已经在超过50多台其他机器上运行了相同的代码(大部分是vista或windows 7),所以它看起来像是这个盒子特有的东西。对我可以尝试的建议/想法?
的更多信息: 所以,我的计算机上运行下面的代码,这会导致同样的例外,但在for循环声明
MessageBox.Show("pre setup"); // displays correctly
ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2");
//connect to the machine
scope.Connect();
MessageBox.Show("scope setup"); // displays correctly
//use a SelectQuery to tell what we're searching in
SelectQuery searchQuery = new SelectQuery("SELECT * FROM Win32_Processor");
//set the search up
ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope, searchQuery);
MessageBox.Show("search object setup"); // displays correctly
//get the results into a collection
ManagementObjectCollection obj = searcherObj.Get();
MessageBox.Show("got ManagementObjectCollection"); // displays correctly
// next statement appears to cause Invalid class exception
foreach (ManagementObject mo in obj)
{
try
{
MessageBox.Show("looking for device id, cpu0"); // never shows up
if (string.Equals((string) mo["DeviceID"], "CPU0", StringComparison.InvariantCultureIgnoreCase))
{
MessageBox.Show("processor ID: " + mo["ProcessorID"].ToString()); // never shows up
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Exception fetching processor id: " + ex.ToString()); // doesn't show
}
}
其他帮助?
没能得到这个想通了,它似乎认为Johnv2020建议有在Windows XP SP3中的错误或问题,它不允许我以获取处理器ID的网站。作为参考,处理器在这台机器上是一台i5 650,并且我通过在try catch中包装问题并在此情况下忽略处理器ID来解决此问题。特别感谢Johnv2020的帮助!
嗨 - 看看这个链接似乎表明,它与CPU类型和SP3的一个问题 - HTTP://新闻.softpedia.com/news/XP-SP3-Win32-Processor-Class-Labels-Intel-Core-2-Duo-CPUs-Incorectly -90201.shtml – Johnv2020 2011-05-03 22:15:11
Great link !!!我现在正在研究这个! – Beau 2011-05-04 14:09:03
博,只是一个小的要求,你可以标记我的答案是有用和/或接受请。它只是让其他人知道有一个解决方案。谢谢 – Johnv2020 2011-05-06 21:04:44