Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when laptop power cable has been disconnected?

Tags:

c#

.net

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?

like image 733
Epaga Avatar asked Dec 07 '08 18:12

Epaga


People also ask

Why is my laptop not recognizing my power cord?

Check the AC adapter and cables (including the power cable) for any signs of physical damage or overheating. Check if the pin inside the plug or adapter port is bent, broken, or damaged.

How do I know if my power adapter is broken?

If you've plugged your computer into an outlet, but notice it isn't charging, or if your computer shuts itself down while it's plugged in, you may have a faulty AC adapter. Plug your AC adapter into a different outlet and see if your laptop charges.


1 Answers

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

like image 122
mdb Avatar answered Sep 20 '22 07:09

mdb