在WMI C#中充分利用剩余电量为电池充电

时间:2017-07-09 22:25:41

标签: c# wmi

我将 Win32_Battery class与C#一起使用,以便在几分钟内获得剩余时间为电池充电。但它对我不起作用。

编辑:没有例外,只返回空值。 enter image description here

我的代码:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_Battery"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Battery instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("TimeToFullCharge: {0}", queryObj["TimeToFullCharge"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

0 个答案:

没有答案