-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
233 lines (196 loc) · 7.36 KB
/
Program.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace MyTrayApp
{
public class SysTrayApp : Form
{
[STAThread]
public static void Main()
{
Application.Run(new SysTrayApp());
}
private readonly NotifyIcon trayIcon;
private readonly ContextMenu trayMenu;
private readonly string appName = "WinToLinux";
List<string> uefi = new List<string>();
List<string> uuid = new List<string>();
string bootsequence;
string currentValue;
int shift;
readonly Regex regexUUID = new Regex("^(\\{){0,1}[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}(\\}){0,1}$");
public SysTrayApp()
{
GetMenuItems();
currentValue = bootsequence ?? uuid.First();
shift = uuid.Count() - uefi.Count();
// Create a simple tray menu with only one item.
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Settings").Enabled = false;
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isTaskEnable();
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Reboot to...").Enabled = false;
trayMenu.MenuItems.Add("-");
foreach (var pos in uefi.Select((value, i) => new { i, value }))
{
MenuItem item = new MenuItem
{
Checked = uuid[pos.i + shift] == currentValue,
Tag = uuid[pos.i + shift],
Text = pos.value
};
item.Click += OnMenuClick;
trayMenu.MenuItems.Add(item);
}
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Reboot system", OnReboot);
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Exit", OnExit);
// Create a tray icon. In this example we use a
// standard system icon for simplicity, but you
// can of course use your own custom icon too.
trayIcon = new NotifyIcon
{
Text = "Reboot to Linux",
Icon = WinToLinux.Properties.Resources.WtL,
// Add menu to tray icon and show it.
ContextMenu = trayMenu,
Visible = true
};
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void OnMenuClick(object sender, EventArgs e)
{
string UUID = (sender as MenuItem).Tag.ToString();
string command = "/C bcdedit.exe /set {fwbootmgr} bootsequence " + UUID + " /addfirst";
Console.WriteLine(command);
LaunchCMD(command);
uuid = new List<string>();
uefi = new List<string>();
LaunchCMD("/C bcdedit /enum firmware");
currentValue = bootsequence ?? uuid.First();
shift = uuid.Count() - uefi.Count();
foreach (var pos in uefi.Select((value, i) => new { i, value }))
{
trayMenu.MenuItems[pos.i + 6].Checked = uuid[pos.i + shift] == currentValue;
}
}
private void CreateTask()
{
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "WinToLinux. Start on boot";
td.Triggers.Add(new LogonTrigger());
td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null));
td.Principal.RunLevel = TaskRunLevel.Highest;
ts.RootFolder.RegisterTaskDefinition(appName, td);
}
}
private void DeleteTask()
{
using (TaskService ts = new TaskService())
{
if (ts.GetTask(appName) != null)
{
ts.RootFolder.DeleteTask(appName);
}
}
}
private bool isTaskEnable()
{
using (TaskService ts = new TaskService())
{
return (ts.GetTask(appName) != null);
}
}
private void OnRegisterInStartup(object sender, EventArgs e)
{
if (isTaskEnable())
{
DeleteTask();
trayMenu.MenuItems[2].Checked = false;
}
else
{
CreateTask();
trayMenu.MenuItems[2].Checked = true;
}
}
private void OnReboot(object sender, EventArgs e)
{
LaunchCMD("/C shutdown /r /t 0");
}
private void GetMenuItems()
{
LaunchCMD("/C bcdedit /enum firmware");
}
private void LaunchCMD(string command)
{
Process build = new Process();
build.StartInfo.Arguments = command;
build.StartInfo.FileName = "cmd.exe";
build.StartInfo.UseShellExecute = false;
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
build.StartInfo.CreateNoWindow = true;
build.ErrorDataReceived += Build_ErrorAndDataReceived;
build.OutputDataReceived += Build_ErrorAndDataReceived;
build.EnableRaisingEvents = true;
build.Start();
build.BeginOutputReadLine();
build.BeginErrorReadLine();
build.WaitForExit();
}
void Build_ErrorAndDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && e.Data != "")
{
string strMessage = e.Data;
string[] splited = strMessage.Split(' ');
splited = splited.Where(address => !string.IsNullOrWhiteSpace(address)).ToArray();
Match match = regexUUID.Match(splited.Last());
if (splited[0] == "description")
{
splited = splited.Where(w => w != splited[0]).ToArray();
Console.WriteLine(String.Join(" ", splited));
uefi.Add(String.Join(" ", splited));
}
if (splited[0] == "bootsequence")
{
Console.Write(splited.Last());
bootsequence = splited.Last();
}
if (splited[0] != "resumeobject" && splited[0] != "bootsequence" && match.Success || splited.Last() == "{bootmgr}")
{
Console.Write(splited.Last());
uuid.Add(splited.Last());
}
}
}
// All below for hide a Form
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
}