Home > C# > Using PowerStatus in System.Windows.Forms.SystemInformation

Using PowerStatus in System.Windows.Forms.SystemInformation

From the definitions of MSDN “PowerStatus” that indicates the current system power status. This is a needed class if you are developing applications that uses mobile devices; This class allows you to gets information about the battery life of a mobile device (in my case ‘laptop’).

I however do not feel convinced placing this class under “System.Windows.Forms” namespace and could have been under “System.Diagnostics” Namespace; Anyways may be its because “System.Windows.Forms” had “SystemInformation”.

An example program is given below: (If you are trying this in console program, dont forgot to add reference to “System.Windows.Forms”)

using System;
using System.Windows.Forms;

namespace PowerStatusConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            string strBatteryChargeStatus, strBatteryFullLifetime, strBatteryLifePercent, strBatteryLifeRemaining, strPowerLineStatus;
            strBatteryChargeStatus = strBatteryFullLifetime = strBatteryLifePercent = strBatteryLifeRemaining = strPowerLineStatus = string.Empty;

            // Getting the current battery charge status. 
            switch (SystemInformation.PowerStatus.BatteryChargeStatus)
            {
                case BatteryChargeStatus.Charging:
                    strBatteryChargeStatus = "BatteryChargeStatus: Charging";
                    break;
                case BatteryChargeStatus.Critical:
                    strBatteryChargeStatus = "BatteryChargeStatus: Critical";
                    break;
                case BatteryChargeStatus.High:
                    strBatteryChargeStatus = "BatteryChargeStatus: High";
                    break;
                case BatteryChargeStatus.Low:
                    strBatteryChargeStatus = "BatteryChargeStatus: Low";
                    break;
                case BatteryChargeStatus.NoSystemBattery:
                    strBatteryChargeStatus = "BatteryChargeStatus: NoSystemBattery";
                    break;
                case BatteryChargeStatus.Unknown:
                    strBatteryChargeStatus = "BatteryChargeStatus: Unknown";
                    break;
            }

            // Getting the reported full charge lifetime of the primary battery power source in seconds.
            strBatteryFullLifetime = "BatteryFullLifetime: " + SystemInformation.PowerStatus.BatteryFullLifetime.ToString();

            // Getting the approximate percentage of full battery time remaining.
            strBatteryLifePercent = "BatteryLifePercent: " + (100 * SystemInformation.PowerStatus.BatteryLifePercent).ToString();

            // Getting the approximate number of seconds of battery time remaining.
            strBatteryLifeRemaining = "BatteryLifeRemaining: " + (TimeSpan.FromSeconds(SystemInformation.PowerStatus.BatteryLifeRemaining).Minutes).ToString();

            // Getting the current system power status.
            switch (SystemInformation.PowerStatus.PowerLineStatus)
            {
                case PowerLineStatus.Offline:
                    strPowerLineStatus = "PowerLineStatus: Offline";
                    break;
                case PowerLineStatus.Online:
                    strPowerLineStatus = "PowerLineStatus: Online";
                    break;
                case PowerLineStatus.Unknown:
                    strPowerLineStatus = "PowerLineStatus: Unknown";
                    break;
            }

            Console.WriteLine(strBatteryChargeStatus);
            Console.WriteLine(strBatteryFullLifetime);
            Console.WriteLine(strBatteryLifePercent);
            Console.WriteLine(strBatteryLifeRemaining);
            Console.WriteLine(strPowerLineStatus);
            Console.ReadLine();

        }
    }
Advertisements
  1. January 29, 2011 at 4:47 AM

    i appreciate this post of yours because i needed this peice of information.i have added your post to my mixx account.

  2. August 7, 2011 at 7:14 AM

    It’s appropriate time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions. Perhaps you could write next articles referring to this article. I desire to read even more things about it!

  3. February 22, 2012 at 2:51 AM

    Using PowerStatus in System.Windows.Forms.SystemInformation Code Support I was suggested this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks! your article about Using PowerStatus in System.Windows.Forms.SystemInformation Code Support Best Regards Shane Yoder

  4. February 22, 2012 at 9:38 AM

    I’m not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this Using PowerStatus in System.Windows.Forms.SystemInformation Code Support for my mission.

  5. February 24, 2012 at 7:27 AM

    An exciting discourse is worth annotate. I guess that you should indite writer on this message, it might not be a bias master but generally fill are not enough to mouth on much topics. To the next. Cheers like your Using PowerStatus in System.Windows.Forms.SystemInformation Code Support.

  6. March 2, 2012 at 1:43 PM

    I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this Using PowerStatus in System.Windows.Forms.SystemInformation Code Support for my mission.

  7. March 3, 2012 at 2:36 AM

    Wanted posting. Loads of outstanding writing here. I wish I saw it found the internet site sooner. Congrats!

  8. March 5, 2012 at 8:27 PM

    brilliantly insightful post. If only it was as straightforward to implement some with the solutions as it was to read and nod my head at each of your points

  9. May 9, 2012 at 9:09 AM

    Great site! I am loving it!! Will come back again. I am bookmarking your feeds also

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: