CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – PowerStatus – Display power status information

Have you noticed, Battery icon in the System Tray in your Windows System? This shows, how much power remains, whether the power plug is plugged in and some other useful information to alert you to set the Power Options.

In this article, we are going to discuss how to show basic power options by writing our own C# code.

.Net provides a class PowerStatus, to get current system power status information. It has below useful properties to provide power information:

BatteryChargeStatus provides the current battery charge status. It is an enumeration, provides battery information; whether it is charging, high level of battery charge, low level of battery charge, etc.

PowerLineStatus property provides whether the power cable is plugged in (Online) or not plugged in (Offline). It is also an enumeration.

BatteryLifePercent field gives an approximate amount of full battery charge remaining. This is the float value.

BatteryLifeRemaining property tells approximately the amount of battery life remaining. This is an int value. And the value it returns in seconds.

Interestingly, you can’t create PowerStatus class’s object directly as it doesn’t have a constructor to create its’ object. Then, how do we create the object of it? .Net provides another useful class, which is SystemInformation class; gives information about the current system environment. It has a number of properties for different purposes, and one of them is PowerStatus; which gives the current power status.

That means SystemInformation.PowerStatus is the PowerStatus‘s object.

Lets’ write a simple program to display the power status information.

Step 1. Create a Windows Forms Application.

Step 2. Add a button to the form, and name it “Show“. Add a click button handler to it.

Step 3. Add a text box to the form; where we will display the power status information when the user clicks on the “Show” button.

Step 4. Add the code in button click handler to show power status information. The complete code looks like below:

Step 5. Build and Run the Application. Click on the Show button to show the status of the current system battery.

Battery Status Information
Battery Status Information

Observe the results; it shows the current status of the battery of the System.

Please add your review comments if you like this Article.

(Raju)

C# – PowerStatus – Display power status information

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top