WEB开发网
开发学院软件开发C++ 获取计算机电源或者电池状态 阅读

获取计算机电源或者电池状态

 2012-05-19 17:05:12 来源:WEB开发网   
核心提示:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;/*By 玛丽隔壁*/namespace PowerStatus{ ///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

/*By 玛丽隔壁*/

namespace PowerStatus
{
    /// <summary>
    /// System Power Status
    /// </summary>
    public struct _SYSTEM_POWER_STATUS
    {
        /// <summary>
        /// The AC power status. This member can be one of the following values.
        /// 0 Offline
        /// 1 Online
        /// 255 Unknown status
        /// </summary>
        public byte ACLineStatus;
        /// <summary>
        /// The battery charge status. This member can contain one or more of the following flags.
        /// 1 High—the battery capacity is at more than 66 percent
        /// 2 Low—the battery capacity is at less than 33 percent
        /// 4 Critical—the battery capacity is at less than five percent
        /// 8 Charging
        /// 128 No system battery
        /// 255 Unknown status—unable to read the battery flag information
        /// The value is zero if the battery is not being charged and the battery capacity is between low and high.
        /// </summary>
        public byte BatteryFlag;
        /// <summary>
        /// The percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or 255 if status is unknown.
        /// </summary>
        public byte BatteryLifePercent;
        /// <summary>
        /// Reserved; must be zero.
        /// </summary>
        public byte Reserved1;
        /// <summary>
        /// The number of seconds of battery life remaining, or –1 if remaining seconds are unknown.
        /// </summary>
        public int BatteryLifeTime;
        /// <summary>
        /// The number of seconds of battery life when at full charge, or –1 if full battery lifetime is unknown.
        /// </summary>
        public int BatteryFullLifeTime;

    }

    public class PowerStatus
    {
        /// <summary>
        /// GetSystemPowerStatus Function
        /// </summary>
        /// <param name="status">out System Power Status</param>
        /// <returns></returns>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto), System.Security.SuppressUnmanagedCodeSecurity]
        public static extern bool GetSystemPowerStatus(ref _SYSTEM_POWER_STATUS status);
    }
}

调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*By 玛丽隔壁*/

namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            PowerStatus._SYSTEM_POWER_STATUS pst = new PowerStatus._SYSTEM_POWER_STATUS();
            PowerStatus.PowerStatus.GetSystemPowerStatus(ref pst);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("ACLineStatus:" + pst.ACLineStatus);
            Console.WriteLine("BatteryFlag:" + pst.BatteryFlag);
            Console.WriteLine("BatteryFullLifeTime:" + pst.BatteryFullLifeTime);
            Console.WriteLine("BatteryLifePercent:" + pst.BatteryLifePercent);
            Console.WriteLine("BatteryLifeTime:" + pst.BatteryLifeTime);
            Console.ReadKey();

        }
    }
}

点击下载此文件

Tags:获取 计算机 电源

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接