For the umpteenth time my laptop just shut down in the middle of my game because my power cable had disconnected without me noticing it.
Now I want to write a little
This may very much depend on your exact operating system. Here are some calls for windows XP, I am sure you can find the Vista equivalents:
http://msdn.microsoft.com/en-us/library/ms704147(VS.85).aspx
This should be trivial to implement using the SystemInformation.PowerStatus property. And even though that lives in Windows.Forms, it should be perfectly usable from a system service.
For a solution that also works on the Compact Framework, see HOWTO: Get the Device Power Status
Can't think of anything directly accessible via the .NET framework, but I do know that Intel has the Mobile Platform SDK with .NET libraries that should provide this information to you. It's possible AMD has an equivilent somewhere.
Intel Mobile Platform SDK
Here, is a little solution: using C# windows form application,
PowerStatus powerStatus = SystemInformation.PowerStatus;
if (powerStatus.PowerLineStatus == PowerLineStatus.Online)
{
MessageBox.Show("Running On Power", Convert.ToString(powerStatus.BatteryLifePercent * 100) + "%");
}
else
{
MessageBox.Show("Running On Battery", Convert.ToString(powerStatus.BatteryLifePercent * 100) + "%");
}
Hope, you got the idea, now you can use it in any way,...........
To continue BQ's answer, there are power settings in Windows that can be changed when the power cord is removed. Since I sometimes work with it removed I didn't change the power settings on my machine, but when the battery approaches 15% the screen brightness is set to the lowest level, making it possible to work (and easily changeable with Fn-Home key) but very noticeable so that I plug the cable back in time.
SystemEvents.PowerModeChanged. You will need to use either GetSystemPowerStatus (see link in one of the answers) or SystemInformation.PowerStatus (link is in another answer) in the handler to check what happened.