Exemple #1
0
        /// <summary>
        /// Gets the status of battery.
        /// </summary>
        /// <returns>Battery status.</returns>
        /// <exception cref="PowerManagerException">
        /// Call to native API has raised an error.
        /// </exception>
        public BatteryStatus GetBatteryStatus()
        {
            bool systemPowerStatus = false;
            BatteryStatus batteryStatus = new BatteryStatus();

            try
            {
                SystemPowerStatus powerStatus = new SystemPowerStatus();

                //Get BatteryLifePercent and BatteryFlag values
                systemPowerStatus =
                    (PowerManagementNativeMethods.GetSystemPowerStatus(out powerStatus));

                batteryStatus.BatteryLifePercent = powerStatus.BatteryLifePercent;
                batteryStatus.BatteryFlag = (BatteryFlag)powerStatus.BatteryFlag;

                if ((powerStatus.BatteryFlag & (byte)BatteryFlag.Charging) ==
                    (byte)BatteryFlag.Charging)
                {
                    batteryStatus.Charging = true;
                }
                else
                {
                    batteryStatus.Charging = false;
                }

                if ((powerStatus.BatteryFlag & (byte)BatteryFlag.NoSystemBattery) ==
                   (byte)BatteryFlag.NoSystemBattery)
                {
                    batteryStatus.BatteryPresent = false;
                }
                else
                {
                    batteryStatus.BatteryPresent = true;
                }
            }
            catch (Exception exception)
            {
                throw new PowerManagerException(exception.Message, exception);
            }
            if (!systemPowerStatus)
            {
                throw new PowerManagerException(PowerManagementResource.Win32ErrorCodeMessage
                    + Marshal.GetLastWin32Error());
            }

            return batteryStatus;
        }
 internal static extern bool GetSystemPowerStatus(
     out SystemPowerStatus sysPowerStatus);
Exemple #3
0
 internal static extern bool GetSystemPowerStatus(
     out SystemPowerStatus sysPowerStatus);