-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
364 lines (291 loc) · 8.54 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
364 lines (291 loc) · 8.54 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Principal;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows.Media.Media3D;
using System.Linq.Expressions;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace WinContextTweaker
{
public partial class MainWindow : Window
{
public string? path;
public string? selectedScript;
public MainWindow()
{
if (IsElevated())
{
InitializeComponent();
CanEdit(false);
CanSee(false);
cmbContext.SelectedIndex = 0;
}
else
{
MessageBox.Show("You need to run this program with Elevated Permissions to edit the Registry!");
this.Close();
}
}
static private bool IsElevated()
{
bool isElevated;
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return isElevated;
}
private void UpdateOptions()
{
RegistryKey? script = OpenRootSubKey(path + "shell\\" + selectedScript);
if (script != null)
{
foreach (CheckBox checkbox in stkOptions.Children)
{
checkbox.IsChecked = script.GetValue(checkbox.DataContext as string) != null;
}
}
}
private void UpdateScripts()
{
lstScripts.Items.Clear();
if (path != null)
{
string[] subKeyNames = OpenRootSubKey(path + "shell\\")?.GetSubKeyNames() ?? [];
//Debug.WriteLine(string.Join(" ", subKeyNames));
foreach (string name in subKeyNames)
{
if (GetRootScriptCommand(path + "shell\\" + name) != null)
{
lstScripts.Items.Add(name);
}
}
lblNoScriptFound.Visibility = lstScripts.HasItems ? Visibility.Collapsed : Visibility.Visible;
}
}
private void CanSee(bool allow)
{
Visibility visibility = allow ? Visibility.Visible : Visibility.Collapsed;
stkCommand.Visibility = visibility;
stkOptions.Visibility = visibility;
}
private void CanEdit(bool allow)
{
txtCommand.IsEnabled = allow;
stkOptions.IsEnabled = allow;
btnRenameScript.IsEnabled = allow;
btnDeleteScript.IsEnabled = allow;
}
static private RegistryKey? OpenRootSubKey(string name, bool writable = false)
{
return Registry.ClassesRoot.OpenSubKey(name, writable);
}
static private RegistryKey? CreateRootSubKey(string path, string key)
{
return OpenRootSubKey(path, true)?.CreateSubKey(key);
}
static private void DeleteRootSubKeyTree(string path, string key)
{
OpenRootSubKey(path, true)?.DeleteSubKeyTree(key);
}
static private string? GetRootScriptCommand(string path)
{
RegistryKey? key = OpenRootSubKey(path + "\\command");
if (key == null) return null;
return key.GetValue("") as string ?? "";
}
static private void SetRootScriptCommand(string path, string command)
{
OpenRootSubKey(path + "\\command", true)?.SetValue("", command);
}
static private void RenameRootScript(string path, string oldKey, string newKey)
{
RegistryKey? oldScript = OpenRootSubKey(path + oldKey);
if (oldScript != null && OpenRootSubKey(path + newKey) == null)
{
RegistryKey? newScript = CreateRootSubKey(path, newKey);
CreateRootSubKey(path, newKey + "\\command");
if (newScript != null)
{
string command = GetRootScriptCommand(path + oldKey) ?? "";
SetRootScriptCommand(path + newKey, command);
string[] enabledOptions = oldScript.GetValueNames();
foreach (string name in enabledOptions)
{
newScript.SetValue(name, "");
}
DeleteRootSubKeyTree(path, oldKey);
}
}
}
static private void SwitchScriptOption(string path, string option, bool enable, string value = "")
{
RegistryKey? script = OpenRootSubKey(path, true);
if (script != null)
{
if (enable)
{
script.SetValue(option, value);
}
else if (script.GetValue(option) != null)
{
script.DeleteValue(option);
}
}
}
static private bool IsValidRegexKeyName(string str)
{
const string invalidChars = """\""";
Regex InvalidCharsRegex = new Regex($"[{Regex.Escape(invalidChars)}]");
if (InvalidCharsRegex.IsMatch(str) || str.Length == 0) return false;
return true;
}
private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.F5:
UpdateScripts();
UpdateOptions();
break;
}
}
private void Context_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CanEdit(false);
ComboBoxItem? selectedItem = e.AddedItems[0] as ComboBoxItem;
if (selectedItem == null) return;
path = selectedItem.DataContext as string;
if (path == "SystemFileAssociations\\")
{
lstScripts.Items.Clear();
InputDialog inputDialog = new InputDialog("Enter the file extension (WITH A POINT)", ".");
if (inputDialog.ShowDialog() == true)
{
path += inputDialog.Answer + "\\";
lblSelectedExtension.Content = inputDialog.Answer;
CreateRootSubKey("SystemFileAssociations", inputDialog.Answer);
UpdateScripts();
lblSelectedExtension.Content = inputDialog.Answer;
lblSelectedExtension.Visibility = Visibility.Visible;
}
else
{
cmbContext.SelectedIndex = 0;
}
}
else
{
lblSelectedExtension.Visibility = Visibility.Collapsed;
UpdateScripts();
}
}
private void List_ScriptChanged(object sender, EventArgs e)
{
selectedScript = lstScripts.SelectedItem as string;
if (selectedScript == null)
{
CanEdit(false);
CanSee(false);
}
else
{
CanSee(true);
try
{
UpdateOptions();
string? command = GetRootScriptCommand(path + "shell\\" + selectedScript);
txtCommand.Text = command;
CanEdit(true);
}
catch
{
CanEdit(false);
}
}
}
private void Checkbox_SwitchScriptOption(object sender, EventArgs e)
{
CheckBox element = (CheckBox)sender;
bool state = element.IsChecked ?? false;
string? option = element.DataContext as string;
if (option == null) return;
string value = "";
if (option == "MultiSelectModel") value = "player";
SwitchScriptOption(path + "shell\\" + selectedScript, option, state, value);
if (option == "Icon" && state == true)
{
element.IsChecked = false;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "%userprofile%";
openFileDialog.Filter = "Icon Files (*.ico, *.exe)|*.ico;*.exe";
openFileDialog.FilterIndex = 0;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == true)
{
string iconPath = openFileDialog.FileName;
OpenRootSubKey(path + "shell\\" + selectedScript, true)?.SetValue(option, iconPath);
element.IsChecked = true;
}
}
}
private void Text_CommandChanged(object sender, EventArgs e)
{
string command = txtCommand.Text;
SetRootScriptCommand(path + "shell\\" + selectedScript, command);
}
private void Button_NewScript(object sender, EventArgs e)
{
const string defaultCommand = "cmd /c \"echo Hello World && pause\"";
InputDialog inputDialog = new InputDialog("Enter the new script's name:", "");
if (inputDialog.ShowDialog() == true && path != null)
{
if (IsValidRegexKeyName(inputDialog.Answer))
{
CreateRootSubKey(path, $"shell\\{inputDialog.Answer}\\command");
SetRootScriptCommand(path + "shell\\" + inputDialog.Answer, defaultCommand);
UpdateScripts();
}
}
}
private void Button_DeleteScript(object sender, EventArgs e)
{
Prompt prompt = new Prompt("Are you sure you want to delete this script?");
if (prompt.ShowDialog() == true && path != null && selectedScript != null)
{
DeleteRootSubKeyTree(path + "shell\\", selectedScript);
UpdateScripts();
}
}
private void Button_RenameScript(object sender, EventArgs e)
{
if (selectedScript == null) return;
InputDialog inputDialog = new InputDialog("Enter the script's new name:", selectedScript);
if (inputDialog.ShowDialog() == true && path != null && selectedScript != null)
{
if (IsValidRegexKeyName(inputDialog.Answer))
{
RenameRootScript(path + "shell\\", selectedScript, inputDialog.Answer);
UpdateScripts();
}
}
}
}
}