15

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 C# program that detects when my power cable disconnects and then emits a nice long System beep. What API could I use for that?

8 Answers 8

11

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

2
  • 1
    The link is not working now, please update if possible.
    – Venkat
    Jan 28, 2019 at 14:25
  • Is there anyway to find the information from windows command line? Aug 10, 2020 at 19:20
11

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.

2
  • your ans was just what I needed thanx :) Sep 15, 2013 at 18:53
  • This is awesome and should be the accepted answer. The question was to detect when it happens, not monitor it constantly until it does. Well done. Thanks. Aug 3, 2019 at 21:02
2

This will probably get closed as not-programming-related, but you may want to check your BIOS setttings.

My Lenovo laptop does exactly what you're suggesting natively. There is a setting in my BIOS for beeping on power status change.

1
  • 2
    Notice how he cleverly worded it as a programming question. I can imagine other circumstances where a program would want to know if it was running on battery power, so it's a good question to have in the database. Dec 7, 2008 at 19:25
2

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.

2

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,...........

1

For Windows and .NET 6+, I wrote a wrapper around the win32 System Power Status APIs. My code not only wraps GetSystemPowerStatus in a convenient way, but also lets you subscribe to OS events to be notified when:

  • The power source changes
  • Battery percentage changes
  • Battery saver turns on/off
  • Current monitor's display state changes
  • The lid opens/closes
  • The power scheme changes
  • The primary monitor turns on/off
  • The session display's state changes
  • The system enters/leaves "away mode"
  • The system is not idle
  • The user is/isn't active

https://www.nuget.org/packages/PowerStatus

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.