Windows 电源管理是现代应用程序中非常重要的功能,它可以帮助开发者控制系统电源状态,优化应用程序的能耗和性能。在 C# 中,我们可以通过 Microsoft.Win32
命名空间和 System.Windows.Forms
中的相关类来实现电源管理功能。
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AppPower
{
public class PowerStatusMonitor
{
/// <summary>
/// 获取当前电池状态
/// </summary>
public (float,string,int) MonitorBatteryStatus()
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
// 电池电量百分比
float batteryPercentage = powerStatus.BatteryLifePercent * 100;
// 电池充电状态
string chargeStatus = powerStatus.BatteryChargeStatus.ToString();
// 剩余电池时间
int remainingMinutes = powerStatus.BatteryLifeRemaining;
return (batteryPercentage, chargeStatus, remainingMinutes);
}
}
}
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace AppPowerControl
{
public enum PowerMode
{
Unknown,
Balanced,
HighPerformance,
PowerSaver
}
public class PowerModeManager
{
// Windows API 导入
[DllImport("powrprof.dll")]
public static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent);
[DllImport("PowrProf.dll")]
public static extern uint PowerGetActiveScheme(IntPtr UserRootPowerKey, out IntPtr ActivePolicyGuid);
[DllImport("PowrProf.dll")]
public static extern uint PowerSetActiveScheme(IntPtr UserRootPowerKey, ref Guid SchemeGuid);
// 预定义的电源方案GUID
private static readonly Guid GUID_BALANCED_POWER_PLAN = new Guid("381b4222-f694-41f0-9685-ff5bb260df2e");
private static readonly Guid GUID_HIGH_PERFORMANCE_POWER_PLAN = new Guid("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
private static readonly Guid GUID_POWER_SAVER_POWER_PLAN = new Guid("a1841308-3541-4fab-bc81-f71556f20b4a");
public static void SwitchPowerMode(PowerMode mode)
{
try
{
Guid targetScheme;
switch (mode)
{
case PowerMode.Balanced:
targetScheme = GUID_BALANCED_POWER_PLAN;
break;
case PowerMode.HighPerformance:
targetScheme = GUID_HIGH_PERFORMANCE_POWER_PLAN;
break;
case PowerMode.PowerSaver:
targetScheme = GUID_POWER_SAVER_POWER_PLAN;
break;
default:
throw new ArgumentException("Invalid power mode");
}
uint result = PowerSetActiveScheme(IntPtr.Zero, ref targetScheme);
if (result == 0)
{
Console.WriteLine($"Successfully switched to {mode} mode");
}
else
{
Console.WriteLine($"Failed to switch power mode. Error code: {result}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error switching power mode: {ex.Message}");
}
}
public static PowerMode GetCurrentPowerMode()
{
IntPtr activeScheme;
PowerGetActiveScheme(IntPtr.Zero, out activeScheme);
if (activeScheme != IntPtr.Zero)
{
Guid currentScheme = (Guid)Marshal.PtrToStructure(activeScheme, typeof(Guid));
if (currentScheme.Equals(GUID_BALANCED_POWER_PLAN))
return PowerMode.Balanced;
if (currentScheme.Equals(GUID_HIGH_PERFORMANCE_POWER_PLAN))
return PowerMode.HighPerformance;
if (currentScheme.Equals(GUID_POWER_SAVER_POWER_PLAN))
return PowerMode.PowerSaver;
}
return PowerMode.Unknown;
}
}
}
C#namespace AppPowerControl
{
internal class Program
{
static void Main(string[] args)
{
// 获取当前电源模式
PowerMode currentMode = PowerModeManager.GetCurrentPowerMode();
Console.WriteLine($"Current power mode: {currentMode}");
// 切换到高性能模式
PowerModeManager.SwitchPowerMode(PowerMode.HighPerformance);
// 切换到节能模式
PowerModeManager.SwitchPowerMode(PowerMode.PowerSaver);
// 切换到平衡模式
PowerModeManager.SwitchPowerMode(PowerMode.Balanced);
Console.ReadLine();
}
}
}
通过合理使用 C# 的电源管理 API,开发者可以为 Windows 应用程序提供更智能、更节能的用户体验。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!