ManagementObjectSearcher Get()方法抛出异常

时间:2011-05-02 20:53:29

标签: c# .net-3.5 windows-xp wmi

我正在尝试运行以下代码:

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计算机。

此计算机上的用户配置为管理员,但未使用“管理员”帐户。

我创建了一个示例项目,其中只包含此代码作为错误证明。我知道通过在两行之间添加简单的show消息,行mos.Get()是抛出错误的行,而异常文本似乎通过堆栈跟踪中的“ManagementObjectEnumerator.MoveNext()”来支持它。无论如何,我对在机器上寻找什么感到茫然。

注意,我已经在超过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的帮助!

2 个答案:

答案 0 :(得分:1)

对我来说没问题,你可以试试下面的代码吗?看看会发生什么

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

答案 1 :(得分:0)

当 WMI 被破坏时,WMI 查询通常会发生无效类异常。 要验证它,请执行以下操作:

  1. 开始-->运行-->Wmimgmt.msc并按回车
  2. 在打开的窗口的最左侧窗格中,右键单击 WMI 控件(本地)并选择属性。
  3. 如果属性窗口显示任何错误消息,则确认 Windows 上的 WMI 已损坏。

查看此链接以修复 Windows 10 上的 WMI: https://www.thewindowsclub.com/how-to-repair-or-rebuild-the-wmi-repository-on-windows-10