2011-05-02 172 views
2

我试图运行下面的代码: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的帮助!

+0

嗨 - 看看这个链接似乎表明,它与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

+0

Great link !!!我现在正在研究这个! – Beau 2011-05-04 14:09:03

+0

博,只是一个小的要求,你可以标记我的答案是有用和/或接受请。它只是让其他人知道有一个解决方案。谢谢 – Johnv2020 2011-05-06 21:04:44

回答

1

工程确定对我来说,你可以试试下面&代码看看会发生什么

//set the scope of this search 
     ManagementScope scope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\cimv2"); 
     //connect to the machine 
     scope.Connect(); 
+0

这两个命令运行完成,没有任何例外的机器..? – Beau 2011-05-02 21:52:58

+0

这很奇怪。以下内容返回 – Johnv2020 2011-05-02 22:04:05

+0

ManagementScope scope = new ManagementScope(@“\\”+ Environment.MachineName + @“\ root \ cimv2”); //连接到机器 scope.Connect(); //使用SelectQuery来告诉我们正在搜索的内容 SelectQuery searchQuery = new SelectQuery(“SELECT * FROM Win32_Processor”); //设置搜索结果 ManagementObjectSearcher searcherObj = new ManagementObjectSearcher(scope,searchQuery); //使用(ManagementObjectCollection obj = searcherObj。)将结果导入集合 。获取()) { – Johnv2020 2011-05-02 22:04:11