Get Laptop battery status using C#
In this article, I will show you how to show battery status of laptop battery like is discharging, charging, full etc. We will do this using WMI because WMI have power to answer questions about battery status .So let's start with WMI.
To access account information of Battery we need to Use System.Management name space. To use this name space first of all we need to add reference to System.management by Project Menu>>Add Reference.
After tis at the top of the code use: using System.Management;
So that you can use of its classes . Now its simple to get information about laptop Battery through querying ManagementObjectSearcher Class .
We can get all information about Laptop Battery through Win32_Battery class.
So we need to query on that class to get related Management object and then we just need to get various properties related to it.
The same thing, how to achieve without using WMI Class?
Here it is,
Then after searching in MSDN, I found PowerStatus class, which is part of System.Windows.Forms namespace, which can be used to get the battery level. Here is a implementation, which shows battery status as well as system connection to AC power.
And see here is the above sample code running on my laptop.
If you feel this is helpful or you like it, Please share this using share buttons available on page.To access account information of Battery we need to Use System.Management name space. To use this name space first of all we need to add reference to System.management by Project Menu>>Add Reference.
After tis at the top of the code use: using System.Management;
So that you can use of its classes . Now its simple to get information about laptop Battery through querying ManagementObjectSearcher Class .
We can get all information about Laptop Battery through Win32_Battery class.
So we need to query on that class to get related Management object and then we just need to get various properties related to it.
Dictionary < int, string > StatusCodes; private void Form1_Load(object sender, EventArgs e) { StatusCodes = new Dictionary < int, string >(); StatusCodes.Add(1, "The battery is discharging"); StatusCodes.Add(2, "The system has access to AC. However, the battery is not necessarily charging"); StatusCodes.Add(3, "Fully Charged"); StatusCodes.Add(4, "Low"); StatusCodes.Add(5, "Critical"); StatusCodes.Add(6, "Charging"); StatusCodes.Add(7, "Charging and High"); StatusCodes.Add(8, "Charging and Low"); StatusCodes.Add(9, "Undefined"); StatusCodes.Add(10,"Partially Charged"); /* Set progress bar values and Properties */ progressBar1.Maximum = 100; progressBar1.Style = ProgressBarStyle.Continuous; timer1.Enabled = true; ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Battery"); foreach (ManagementObject mo in mos.Get()) { lblBatteryName.Text = mo["Name"].ToString(); UInt16 statuscode = (UInt16)mo["BatteryStatus"]; string statusString = StatusCodes[statuscode]; lblBatteryStatus.Text = statusString; } } private void OnTimer_Tick(object sender, EventArgs e) { ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Battery where Name='"+lblBatteryName.Text+"'"); foreach (ManagementObject mo in mos.Get()) { UInt16 statuscode = (UInt16)mo["BatteryStatus"]; string statusString = StatusCodes[statuscode]; lblBatteryStatus.Text = statusString; /* Set Progress bar according to status */ if (statuscode == 4) { progressBar1.ForeColor = Color.Red; progressBar1.Value = 5; } else if (statuscode == 2) { progressBar1.ForeColor = Color.Green; progressBar1.Value = 100; } else if (statuscode == 5) { progressBar1.ForeColor = Color.Red; progressBar1.Value = 1; } } }
The same thing, how to achieve without using WMI Class?
Here it is,
Then after searching in MSDN, I found PowerStatus class, which is part of System.Windows.Forms namespace, which can be used to get the battery level. Here is a implementation, which shows battery status as well as system connection to AC power.
private void btnGetBatteryStatus_Click(object sender, EventArgs e) { PowerStatus status = SystemInformation.PowerStatus; progressBarStatus.Value = (int)(status.BatteryLifePercent * 100); lblBatterLevel.Text = String.Format("{0}%", (status.BatteryLifePercent * 100)); switch (status.PowerLineStatus) { case PowerLineStatus.Online: rdoConnected.Checked = true; break; case PowerLineStatus.Offline: rdoNotConnected.Checked = true; break; default: rdoUnknown.Checked = true; break; } }
And see here is the above sample code running on my laptop.
For Battry Charging details and etc. please viist https://www.facebook.com/just4information link there you can find BatteryChecker app.
ReplyDeletepls how do i do this using wpf and not windows form
ReplyDeleteThank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
ReplyDeleteerp software solutions in chennai.
Excellent post! I heve read your blog it's very interesting and informative. Keep sharing.
ReplyDeleteerp providers in chennai | erp software solutions in chennai
Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it.. Web Designing Training Institute in Chennai | Web Designing Training Institute in Velachery.
ReplyDeleteReally an amazing post..! By reading your blog post i gained more information..Dot Net Training Institute in Chennai | Dot Net Training Institute in Velachery
ReplyDeleteThanks for your useful and interesting Post..
ReplyDeleteSix Sigma Certification Training in Chennai | No.1 Six Sigma Certification Exam Center in Chennai
Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Blueprism training in Pune
Blueprism training in tambaram
Blueprism training in annanagar
Blueprism training in velachery
Blueprism training in marathahalli
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.Devops Training in pune|Devops training in tambaram|Devops training in velachery|Devops training in annanagar
ReplyDeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
ReplyDeletejava training in annanagar | java training in chennai
java training in marathahalli | java training in btm layout
This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
ReplyDeleteData Science training in Chennai
Data science training in bangalore
Data science training in pune
Data science online training
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
ReplyDeleteangularjs online Training
angularjs Training in marathahalli
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
I love the information you provide here and can’t wait to take a look when I get home. I’m surprised at how fast your blog loaded on my cell phone. I’m not even using WIFI, just 3G. Anyways, awesome blog!
ReplyDeleteindustrial course in chennai
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Training in Bangalore | Amazon Web Services Training in bangalore , india
AWS Training in pune | Amazon Web Services Training in Pune, india