我試圖運行下面的代碼: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