-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (97 loc) · 3.4 KB
/
Copy pathProgram.cs
File metadata and controls
117 lines (97 loc) · 3.4 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
using PowerPlanManager.Properties;
using System.Text;
namespace PowerPlanManager
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MyCustomApplicationContext());
}
}
struct PowerPlan
{
public Boolean isActive;
public string guid;
public string name;
}
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
ContextMenuStrip strip = new ContextMenuStrip();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
System.Diagnostics.Debug.WriteLine("Found power plans");
List<PowerPlan> availablePlans = GetAvailablePowerPlans();
foreach (PowerPlan item in availablePlans)
{
System.Diagnostics.Debug.WriteLine(item.name + " - " + item.guid);
strip.Items.Add(new ToolStripMenuItem(item.name, null, (sender, e) => ActivatePowerPlan(item)));
}
strip.Items.Add(new ToolStripMenuItem("Exit", null, Exit));
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenuStrip = strip,
Visible = true,
Text = availablePlans.Find(plan => plan.isActive).name
};
}
void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
Dispose();
}
List<PowerPlan> GetAvailablePowerPlans()
{
string[] lines = RunCommand("powercfg", "/list").Split('\n');
List<PowerPlan> result = new();
foreach (string line in lines)
{
if (!line.Contains("GUID"))
{
continue;
}
result.Add(GetPowerPlanFromString(line));
}
return result;
}
PowerPlan GetPowerPlanFromString(string inputString)
{
PowerPlan temp = new PowerPlan();
temp.guid = inputString.Substring(inputString.IndexOf(':')+2, 36);
int startIndex = inputString.IndexOf('(') + 1;
int endIndex = inputString.IndexOf(')');
temp.name = inputString.Substring(startIndex, endIndex - startIndex);
temp.isActive = inputString.Contains('*');
return temp;
}
void ActivatePowerPlan(PowerPlan plan)
{
RunCommand("powercfg", "/setactive " + plan.guid);
UpdateActivePlan(plan);
}
string RunCommand(string command, string arguments)
{
System.Diagnostics.Process pProcess = new();
pProcess.StartInfo.FileName = command;
pProcess.StartInfo.Arguments = arguments;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.Start();
//Get program output
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
return output;
}
void UpdateActivePlan(PowerPlan plan)
{
trayIcon.Text = plan.name;
}
}
}