How to detect when laptop power cable has been disconnected?

前端 未结 7 2114
终归单人心
终归单人心 2020-12-05 15:53

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

相关标签:
7条回答
  • 2020-12-05 16:03

    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 讨论(0)
  • 2020-12-05 16:04

    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

    0 讨论(0)
  • 2020-12-05 16:05

    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

    0 讨论(0)
  • 2020-12-05 16:14

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

    0 讨论(0)
  • 2020-12-05 16:15

    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.

    0 讨论(0)
  • 2020-12-05 16:22

    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.

    0 讨论(0)
提交回复
热议问题