C# Programming

[C#] 노트북 배터리 정보(Notebook Battery)

ZerosKD 2020. 10. 31. 12:07
반응형

* C# 노트북 배터리 정보 예제...

 

Main

 

- 사용한 컨트롤 : Button 2개, TextBox 2개, Label 3개, 프로그래스바 1개, Timer 1개

 

전체 소스 코드

Form1.cs

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CSharp_노트북전원상태
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            timer1.Interval = 1000; //1초 마다...

        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            timer1.Stop();
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            PowerStatus  psStatus = SystemInformation.PowerStatus;

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

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

            //충전 비율
            if (psStatus.BatteryLifePercent != 255)
            {
                pbCharge.Value = (int)(psStatus.BatteryLifePercent * 100);
            }
            else
            {
                pbCharge.Value = 0;
            }

            //잔여 사용 시간
            if (psStatus.BatteryLifeRemaining != -1)
            {
                textBox1.Text = TimeSpan.FromSeconds(psStatus.BatteryLifeRemaining).ToString();
            }
            else
            {
                textBox1.Text = "-------";
            }
            //완충시 사용 시간
            if (psStatus.BatteryFullLifetime != -1)
            {
                textBox2.Text = psStatus.BatteryFullLifetime.ToString();
            }
            else
            {
                textBox2.Text = "-------";
            }


        }
    }
}

 

 

*예제 결과

 

반응형