2013-05-20 32 views
1

我想創建一個應用程序的Windows 8使用C#顯示我當前的電池電量。我試圖查詢win32_battery類的相關屬性,但我得到了一個不尋常的結果。這裏是我的代碼:試圖訪問電池級別,C#

private void btn1_Click(object sender, EventArgs e) 
{ 
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Battery"); 
    ManagementObjectCollection collection = searcher.Get(); 
    foreach (ManagementObject obj in collection) 
    { 
     txtBox.AppendText(obj.ToString() + "\r\n"); 
    }; 
} 

我在txtBox唯一的結果就是

\\MIKESLAPTOP\root\cimv2:Win32_Battery.DeviceID=" ASUSTeKX401-44" 

任何想法,爲什麼我只是讀theDevideID財產?所有的指導非常感謝。

+0

相關的問題:http://stackoverflow.com/questions/5716559/get-battery-info-without-wmi – Raptor

+0

當您嘗試命令'是什麼顯示的Get-在PowerShell控制檯中的WmiObject Win32_Battery'? –

回答

2

這是預期的輸出。您忘記枚舉查詢的屬性。使它看起來類似於這樣:

foreach (ManagementObject obj in searcher.Get()) { 
     foreach (var prop in obj.Properties) { 
      if (prop.Value != null) { 
       txtBox.AppendText(string.Format("{0} = {1}", prop.Name, prop.Value)); 
      } 
     } 
    } 
+0

謝謝!這個工作完美 – Michael