-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSetPowerMode.cs
More file actions
188 lines (175 loc) · 7.6 KB
/
SetPowerMode.cs
File metadata and controls
188 lines (175 loc) · 7.6 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Configuration;
using System.Runtime.InteropServices;
namespace PowerMode
{
/// <summary>
/// This program allows for setting the Windows "power mode" or "power slider" value from the command line.
/// </summary>
class SetPowerMode
{
/// <summary>
/// Execution starts here.
/// </summary>
/// <param name="args">Command line parameters.</param>
/// <returns>Error status; 0 = success, non-zero = failure.</returns>
static int Main(string[] args)
{
try
{
// Read from App.config.
ReadConfig();
if (args.Length == 0)
{
// Report the current power mode.
uint result = PowerGetEffectiveOverlayScheme(out Guid currentMode);
if (result == 0)
{
Console.WriteLine(currentMode);
if (currentMode == PowerMode.BetterBattery)
{
Console.WriteLine("Better battery");
}
else if (currentMode == PowerMode.BetterPerformance)
{
Console.WriteLine("Better performance");
}
else if (currentMode == PowerMode.BestPerformance)
{
Console.WriteLine("Best performance");
}
}
else
{
return (int)result;
}
}
else if (args.Length == 1)
{
// Attempt to set the power mode.
string parameter = args[0].ToLower();
Guid powerMode;
if (parameter == "/?" || parameter == "-?")
{
Usage();
return 1;
}
else if (parameter == "BetterBattery".ToLower())
{
powerMode = PowerMode.BetterBattery;
}
else if (parameter == "BetterPerformance".ToLower())
{
powerMode = PowerMode.BetterPerformance;
}
else if (parameter == "BestPerformance".ToLower())
{
powerMode = PowerMode.BestPerformance;
}
else
{
try
{
powerMode = new Guid(parameter);
}
catch (Exception)
{
Console.Error.WriteLine("Failed to parse GUID.\n");
Usage();
return 1;
}
}
uint result = PowerSetActiveOverlayScheme(powerMode);
if (result == 0)
{
Console.WriteLine("Set power mode to {0}.", powerMode);
}
else
{
Console.Error.WriteLine("Failed to set power mode.\n");
Usage();
}
return (int)result;
}
else
{
Usage();
return 1;
}
}
catch (Exception exception)
{
// Print error information to the console.
Console.Error.WriteLine("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
Console.WriteLine();
Usage();
return 1;
}
return 0;
}
/// <summary>
/// Print a usage message to the console.
/// </summary>
private static void Usage()
{
Console.WriteLine(
"PowerMode (GPLv3); used to set the active power mode on Windows 10, version 1709 or later\n" +
"https://github.com/AaronKelley/PowerMode\n" +
"\n" +
" PowerMode Report the current power mode\n" +
" PowerMode BetterBattery Set the system to \"better battery\" mode\n" +
" PowerMode BetterPerformance Set the system to \"better performance\" mode\n" +
" PowerMode BestPerformance Set the system to \"best performance\" mode\n" +
" PowerMode <GUID> Set the system to the mode identified by the GUID"
);
}
private static void ReadConfig()
{
if (ConfigurationManager.AppSettings["BetterBatteryGuid"] != null)
{
PowerMode.BetterBattery = new Guid(ConfigurationManager.AppSettings["BetterBatteryGuid"]);
}
if (ConfigurationManager.AppSettings["BetterPerformanceGuid"] != null)
{
PowerMode.BetterPerformance = new Guid(ConfigurationManager.AppSettings["BetterPerformanceGuid"]);
}
if (ConfigurationManager.AppSettings["BestPerformanceGuid"] != null)
{
PowerMode.BestPerformance = new Guid(ConfigurationManager.AppSettings["BestPerformanceGuid"]);
}
}
/// <summary>
/// Contains GUID constants for the different power modes.
/// </summary>
/// <seealso cref="https://docs.microsoft.com/en-us/windows-hardware/customize/desktop/customize-power-slider"/>
private static class PowerMode
{
/// <summary>
/// Better Battery mode.
/// </summary>
public static Guid BetterBattery = new Guid("961cc777-2547-4f9d-8174-7d86181b8a7a");
/// <summary>
/// Better Performance mode.
/// </summary>
public static Guid BetterPerformance = new Guid("3af9B8d9-7c97-431d-ad78-34a8bfea439f");
/// <summary>
/// Best Performance mode.
/// </summary>
public static Guid BestPerformance = new Guid("ded574b5-45a0-4f42-8737-46345c09c238");
}
/// <summary>
/// Retrieves the active overlay power scheme and returns a GUID that identifies the scheme.
/// </summary>
/// <param name="EffectiveOverlayPolicyGuid">A pointer to a GUID structure.</param>
/// <returns>Returns zero if the call was successful, and a nonzero value if the call failed.</returns>
[DllImportAttribute("powrprof.dll", EntryPoint = "PowerGetEffectiveOverlayScheme")]
private static extern uint PowerGetEffectiveOverlayScheme(out Guid EffectiveOverlayPolicyGuid);
/// <summary>
/// Sets the active power overlay power scheme.
/// </summary>
/// <param name="OverlaySchemeGuid">The identifier of the overlay power scheme.</param>
/// <returns>Returns zero if the call was successful, and a nonzero value if the call failed.</returns>
[DllImportAttribute("powrprof.dll", EntryPoint = "PowerSetActiveOverlayScheme")]
private static extern uint PowerSetActiveOverlayScheme(Guid OverlaySchemeGuid);
}
}