Attention: We are retiring the ASP.NET Community Blogs. Learn more >

[.NET 2.0] On Battery or Not

I'm thinking of writing a small utility to manage which of my apps gets started at startup of Windows, depending on if my laptop is running on batteries or not. Normally, when I boot it up on batteries, I don't want to start things such as my blog reader and a few other things.

The thing is it's very simple to detect from .NET if you are running on batteries or not. The SystemInformation type gives you all that and more:

using System;

using System.Windows.Forms;

 

namespace BatteryConsole

{

    class Program

    {

        static void Main(string[] args)

        {

            PowerStatus ps = SystemInformation.PowerStatus;

 

            if(ps.PowerLineStatus == PowerLineStatus.Online)

                Console.Write("Your power cable is connected, and ");

            else

                Console.Write("You are running on batteries, and ");

            Console.WriteLine("your battery level is on {0}", (ps.BatteryLifePercent * 100) + "%");

 

        }

    }

}

I'll get back if I ever get that small program written.

No Comments