2011-05-02 171 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