-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellContextMenuService.cs
More file actions
241 lines (202 loc) · 7.96 KB
/
Copy pathShellContextMenuService.cs
File metadata and controls
241 lines (202 loc) · 7.96 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
using Microsoft.Win32;
using System;
using System.Linq;
using System.Windows.Forms;
namespace c2flux
{
public static class ShellContextMenuService
{
private const string DirectoryShellKeyPath = @"Software\Classes\Directory\shell\c2flux";
private const string DirectoryBackgroundShellKeyPath = @"Software\Classes\Directory\Background\shell\c2flux";
private const string LegacyDirectoryShellKeyPath = @"Software\Classes\Directory\shell\WTF";
private const string LegacyFolderShellKeyPath = @"Software\Classes\Folder\shell\WTF";
private const string LegacyDirectoryBackgroundShellKeyPath = @"Software\Classes\Directory\Background\shell\WTF";
private const string DriveShellParentKeyPath = @"Software\Classes\Drive\shell";
private const string DriveScanVerbName = "c2flux.Scan";
private const string DriveSearchVerbName = "c2flux.Search";
private const string DriveScanShellKeyPath = DriveShellParentKeyPath + @"\" + DriveScanVerbName;
private const string DriveSearchShellKeyPath = DriveShellParentKeyPath + @"\" + DriveSearchVerbName;
private const string LegacyDriveScanShellKeyPath = DriveShellParentKeyPath + @"\WTF";
private const string LegacyDriveSearchShellKeyPath = DriveShellParentKeyPath + @"\WTF.Search";
private const string OriginalDriveVerbOrderValueName = "c2flux.OriginalVerbOrder";
private const string OriginalDriveVerbOrderExistsValueName = "c2flux.OriginalVerbOrderExists";
public static void Apply(
bool scanEnabled,
bool searchEnabled)
{
RemoveLegacyEntries();
if (scanEnabled)
{
RegisterScanEntries();
}
else
{
UnregisterScanEntries();
}
if (searchEnabled)
{
RegisterSearchEntry();
}
else
{
UnregisterSearchEntry();
}
RestoreDriveDefaultVerb();
}
private static void RegisterScanEntries()
{
RegisterShellEntry(
DirectoryShellKeyPath,
"%1",
AppConstants.ShellContextMenuText,
null);
RegisterShellEntry(
DriveScanShellKeyPath,
"%1",
AppConstants.ShellContextMenuText,
null);
RegisterShellEntry(
DirectoryBackgroundShellKeyPath,
"%V",
AppConstants.ShellContextMenuText,
null);
}
private static void RegisterSearchEntry()
{
RegisterShellEntry(
DriveSearchShellKeyPath,
"%1",
"c² flux: Search",
"--search");
}
private static void UnregisterScanEntries()
{
Registry.CurrentUser.DeleteSubKeyTree(DirectoryShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(DriveScanShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(DirectoryBackgroundShellKeyPath, false);
}
private static void UnregisterSearchEntry()
{
Registry.CurrentUser.DeleteSubKeyTree(DriveSearchShellKeyPath, false);
}
private static void RemoveLegacyEntries()
{
Registry.CurrentUser.DeleteSubKeyTree(LegacyDirectoryShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(LegacyFolderShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(LegacyDirectoryBackgroundShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(LegacyDriveScanShellKeyPath, false);
Registry.CurrentUser.DeleteSubKeyTree(LegacyDriveSearchShellKeyPath, false);
}
private static void RegisterShellEntry(
string shellKeyPath,
string pathArgument,
string menuText,
string commandOption)
{
string executablePath = Application.ExecutablePath;
string commandText = "\"" + executablePath + "\"";
if (!string.IsNullOrWhiteSpace(commandOption))
{
commandText += " " + commandOption;
}
commandText += " \"" + pathArgument + "\"";
using RegistryKey shellKey =
Registry.CurrentUser.CreateSubKey(shellKeyPath);
shellKey.SetValue(string.Empty, menuText);
shellKey.SetValue("Icon", executablePath + ",0");
using RegistryKey commandKey =
shellKey.CreateSubKey("command");
commandKey.SetValue(string.Empty, commandText);
}
private static void RestoreDriveDefaultVerb()
{
using RegistryKey driveShellKey =
Registry.CurrentUser.CreateSubKey(DriveShellParentKeyPath);
object originalValueExists =
driveShellKey.GetValue(
OriginalDriveVerbOrderExistsValueName);
if (originalValueExists != null)
{
RestoreOriginalDriveVerbOrder(driveShellKey);
return;
}
string currentDefaultVerb =
driveShellKey.GetValue(
string.Empty,
string.Empty) as string ?? string.Empty;
if (ContainsC2FluxDriveVerb(currentDefaultVerb))
{
driveShellKey.SetValue(
string.Empty,
"open",
RegistryValueKind.String);
}
}
private static bool ContainsC2FluxDriveVerb(
string verbValue)
{
if (string.IsNullOrWhiteSpace(verbValue))
{
return false;
}
return verbValue
.Split(
new[] { ',', ' ' },
StringSplitOptions.RemoveEmptyEntries)
.Any(
verb =>
string.Equals(
verb.Trim(),
DriveScanVerbName,
StringComparison.OrdinalIgnoreCase) ||
string.Equals(
verb.Trim(),
DriveSearchVerbName,
StringComparison.OrdinalIgnoreCase) ||
string.Equals(
verb.Trim(),
"WTF",
StringComparison.OrdinalIgnoreCase) ||
string.Equals(
verb.Trim(),
"WTF.Search",
StringComparison.OrdinalIgnoreCase));
}
private static void RestoreOriginalDriveVerbOrder(
RegistryKey driveShellKey)
{
object originalValueExists =
driveShellKey.GetValue(
OriginalDriveVerbOrderExistsValueName);
if (originalValueExists == null)
{
return;
}
bool hadOriginalValue =
Convert.ToInt32(originalValueExists) != 0;
if (hadOriginalValue)
{
string originalValue =
driveShellKey.GetValue(
OriginalDriveVerbOrderValueName,
string.Empty) as string ?? string.Empty;
driveShellKey.SetValue(
string.Empty,
originalValue,
RegistryValueKind.String);
}
else
{
driveShellKey.DeleteValue(
string.Empty,
false);
}
driveShellKey.DeleteValue(
OriginalDriveVerbOrderValueName,
false);
driveShellKey.DeleteValue(
OriginalDriveVerbOrderExistsValueName,
false);
}
}
}