본문 바로가기

자료

[C#/VB.NET] 노트북 배터리 정보

728x90
Public Class Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        timer1.Interval = 1000 '1초마다...
    End Sub

    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        MyBase.OnClosed(e)
        timer1.Stop()
    End Sub


    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        timer1.Start()
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        timer1.Stop()
    End Sub

    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
        Dim psStatus As PowerStatus = SystemInformation.PowerStatus

        '충전 상태
        txtChargeStatus.Text = psStatus.BatteryChargeStatus.ToString()

        '전원 상태
        txtPoerStatus.Text = psStatus.PowerLineStatus.ToString()

        '충전 비율
        If psStatus.BatteryLifePercent <> 255 Then
            pbCharge.Value = Convert.ToInt32(psStatus.BatteryLifePercent * 100)
        Else
            pbCharge.Value = 0
        End If

        '잔여 사용 시간
        If psStatus.BatteryLifeRemaining <> -1 Then
            textBox1.Text = TimeSpan.FromSeconds(psStatus.BatteryLifeRemaining).ToString()
        Else
            textBox1.Text = "-------"
        End If
        '완충시 사용 시간
        If psStatus.BatteryFullLifetime <> -1 Then
            textBox2.Text = psStatus.BatteryFullLifetime.ToString()
        Else
            textBox2.Text = "-------"
        End If


    End Sub

End Class
728x90