-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerStatus.cs
More file actions
34 lines (29 loc) · 906 Bytes
/
Copy pathPowerStatus.cs
File metadata and controls
34 lines (29 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Runtime.InteropServices;
namespace BatteryDown;
internal static class PowerStatus
{
public static bool IsOnBattery
{
get
{
if (!GetSystemPowerStatus(out var status))
{
throw new InvalidOperationException("无法读取当前电源状态。");
}
return status.ACLineStatus == 0;
}
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetSystemPowerStatus(out SystemPowerStatus systemPowerStatus);
[StructLayout(LayoutKind.Sequential)]
private struct SystemPowerStatus
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte SystemStatusFlag;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
}
}