ManagementObjectSearcher Get() 方法抛出异常

问题描述 投票:0回答:2

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

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

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

我创建了一个示例项目,其中基本上只有这段代码作为错误证明。通过在这两行周围添加简单的显示消息,我知道 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的帮助!

c# .net-3.5 windows-xp wmi
2个回答
0
投票

对我来说工作正常,你可以尝试下面的代码并看看会发生什么吗

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

0
投票

当 WMI 损坏时,WMI 查询上通常会出现 Invalid class 异常。 要验证它,请执行以下操作:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.