How to detect computer is running from battery or line power

.NET programming topics
Post Reply
Cyclops
Lieutenant
Lieutenant
Posts: 71
Joined: Wed Jul 15, 2009 1:48 pm
Location: London

How to detect computer is running from battery or line power

Post by Cyclops » Fri Oct 30, 2009 11:40 am

The program uses the GetSystemPowerStatus API function to get power information. When the program starts, it calls this function and looks at the returned fields to determine whether the computer is running on battery or line power, to get the power status (high, low, critical, charging, no battery, or unknown), and to get information about the battery's total and remaining life time.

Code: Select all

    Private Structure SYSTEM_POWER_STATUS
        Public ACLineStatus As Byte
        Public BatteryFlag As Byte
        Public BatteryLifePercent As Byte
        Public Reserved1 As Byte
        Public BatteryLifeTime As Integer
        Public BatteryFullLifeTime As Integer
    End Structure
    Private Declare Function GetSystemPowerStatus Lib "kernel32" (ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim power_status As SYSTEM_POWER_STATUS
        If GetSystemPowerStatus(power_status) = 0 Then
            lblACStatus.Text = "Error"
        Else
            Select Case power_status.ACLineStatus
                Case 0
                    lblACStatus.Text = "Battery"
                Case 1
                    lblACStatus.Text = "On line"
                Case 255
                    lblACStatus.Text = "Unknown"
            End Select

            Dim txt As String = ""
            If power_status.BatteryFlag And 1 Then txt &= ", High (> 66%)"
            If power_status.BatteryFlag And 2 Then txt &= ", Low (< 33%)"
            If power_status.BatteryFlag And 4 Then txt &= ", Critical (< 5%)"
            If power_status.BatteryFlag And 8 Then txt &= ", Charging"
            If power_status.BatteryFlag And 128 Then txt &= ", No system battery"
            If power_status.BatteryFlag = 255 Then txt &= ", Unknown"
            If txt.Length > 0 Then txt = txt.Substring(2)
            lblBatteryStatus.Text = txt

            If power_status.BatteryFullLifeTime = -1 Then
                lblFullLifetime.Text = "Unknown"
            Else
                lblFullLifetime.Text = power_status.BatteryFullLifeTime & " seconds"
            End If

            If power_status.BatteryLifeTime = -1 Then
                lblRemainingLifetime.Text = "Unknown"
            Else
                lblRemainingLifetime.Text = power_status.BatteryLifeTime & " seconds"
            End If

            If power_status.BatteryLifePercent = 255 Then
                lblPercentLifetime.Text = "Unknown"
            Else
                lblPercentLifetime.Text = power_status.BatteryLifePercent & "%"
            End If
        End If
    End Sub
Post Reply

Return to “.NET Programming”