How to detect when laptop power cable has been disconnected?

前端 未结 7 2107
终归单人心
终归单人心 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: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,...........

提交回复
热议问题