-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminUtils.cs
More file actions
320 lines (261 loc) · 12.6 KB
/
AdminUtils.cs
File metadata and controls
320 lines (261 loc) · 12.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
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
using Microsoft.Win32;
using System.IO;
using System.Security.Principal;
using System.Windows;
namespace WinFolderLock;
internal static partial class AdminUtils
{
private const string ApplicationFolderName = "WinFolderLock";
private const string ResourcesFolderName = "Resources";
private const string ContextMenuKeyPath = @"Software\Classes\Directory\shell\WinFolderLock";
private const string ContextMenuCommandKeyPath = @"Software\Classes\Directory\shell\WinFolderLock\command";
private static readonly string[] InstalledResourceFileNames =
[
"Install.ico",
"Locked.ico",
"LockedWin10.ico",
"LockedWIn11.ico",
"Unlocked.ico"
];
// P/Invoke to notify the shell about association/context menu changes so Explorer refreshes
[System.Runtime.InteropServices.LibraryImport("shell32.dll")]
private static partial void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
private const uint SHCNE_ASSOCCHANGED = 0x08000000;
private const uint SHCNF_IDLIST = 0x0000;
private static void NotifyShell()
{
try { SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero); } catch (Exception ex) { Helper.LogError(ex); }
}
internal static void NotifyShellOfChange(string path)
{
try
{
// Notify about the path itself
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
// Also notify about the parent directory to refresh the folder view
var parentDir = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(parentDir))
{
System.Threading.Thread.Sleep(100); // Give the system a moment to complete extraction
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
}
}
catch (Exception ex) { Helper.LogError(ex); }
}
internal static bool IsRunningAsAdministrator()
{
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
internal static void AddLockFolderContextMenu()
{
var executablePath = GetInstalledExecutablePath();
var iconPath = GetContextMenuIconPath();
Registry.CurrentUser.DeleteSubKeyTree(ContextMenuKeyPath, throwOnMissingSubKey: false);
using var menuKey = Registry.CurrentUser.CreateSubKey(ContextMenuKeyPath);
ArgumentNullException.ThrowIfNull(menuKey);
menuKey.SetValue(string.Empty, "Lock Folder", RegistryValueKind.String);
menuKey.SetValue("Icon", iconPath, RegistryValueKind.String);
using var commandKey = Registry.CurrentUser.CreateSubKey(ContextMenuCommandKeyPath);
ArgumentNullException.ThrowIfNull(commandKey);
commandKey.SetValue(string.Empty, $"\"{executablePath}\" \"%1\"", RegistryValueKind.String);
// Notify shell so the new context menu appears without requiring logout/explorer restart
NotifyShell();
}
internal static void RemoveLockFolderContextMenu()
{
Registry.CurrentUser.DeleteSubKeyTree(ContextMenuKeyPath, throwOnMissingSubKey: false);
NotifyShell();
}
internal static void AddUnlockFolderContextMenu()
{
var executablePath = GetInstalledExecutablePath();
var wflckIconPath = GetInstalledResourcePath("LockedWIn11.ico");
// Register .wflck extension under HKCU so it is visible to the current user without requiring admin.
const string extKey = @"Software\Classes\.wflck";
const string progId = "WinFolderLock.File";
const string progIdKey = @"Software\Classes\WinFolderLock.File";
try
{
Registry.CurrentUser.DeleteSubKeyTree(extKey, throwOnMissingSubKey: false);
Registry.CurrentUser.DeleteSubKeyTree(progIdKey, throwOnMissingSubKey: false);
}
catch (Exception ex) { Helper.LogError(ex); }
using (var ext = Registry.CurrentUser.CreateSubKey(extKey))
{
ArgumentNullException.ThrowIfNull(ext);
ext.SetValue(string.Empty, progId, RegistryValueKind.String);
}
using (var p = Registry.CurrentUser.CreateSubKey(progIdKey))
{
ArgumentNullException.ThrowIfNull(p);
p.SetValue(string.Empty, "WFL Locked Folder", RegistryValueKind.String);
}
// Default icon for the ProgID
using (var defIcon = Registry.CurrentUser.CreateSubKey(progIdKey + "\\DefaultIcon"))
{
ArgumentNullException.ThrowIfNull(defIcon);
defIcon.SetValue(string.Empty, wflckIconPath, RegistryValueKind.String);
}
// Shell open command for ProgID (default action on double-click) - label it "Unlock Folder"
using (var shellOpen = Registry.CurrentUser.CreateSubKey(progIdKey + "\\shell\\open"))
{
ArgumentNullException.ThrowIfNull(shellOpen);
shellOpen.SetValue(string.Empty, "Unlock Folder", RegistryValueKind.String);
}
using (var openCmd = Registry.CurrentUser.CreateSubKey(progIdKey + "\\shell\\open\\command"))
{
ArgumentNullException.ThrowIfNull(openCmd);
openCmd.SetValue(string.Empty, $"\"{executablePath}\" \"%1\"", RegistryValueKind.String);
}
// Permanent unlock as a context menu entry
const string extMenuKey = @"Software\Classes\.wflck\shell\WinFolderLockPermUnlock";
const string extCommandKeyPath = @"Software\Classes\.wflck\shell\WinFolderLockPermUnlock\command";
Registry.CurrentUser.DeleteSubKeyTree(extMenuKey, throwOnMissingSubKey: false);
using var menuKey = Registry.CurrentUser.CreateSubKey(extMenuKey);
ArgumentNullException.ThrowIfNull(menuKey);
menuKey.SetValue(string.Empty, "Permanently Unlock Folder", RegistryValueKind.String);
menuKey.SetValue("Icon", wflckIconPath, RegistryValueKind.String);
using var commandKey = Registry.CurrentUser.CreateSubKey(extCommandKeyPath);
ArgumentNullException.ThrowIfNull(commandKey);
// Command includes a /permunlock switch to indicate permanent unlock behavior
commandKey.SetValue(string.Empty, $"\"{executablePath}\" /permunlock \"%1\"", RegistryValueKind.String);
// Refresh Explorer to show the new context menu entry immediately
NotifyShell();
}
internal static void RemoveUnlockFolderContextMenu()
{
const string extKey = @"Software\Classes\.wflck";
const string progIdKey = @"Software\Classes\WinFolderLock.File";
Registry.CurrentUser.DeleteSubKeyTree(extKey, throwOnMissingSubKey: false);
Registry.CurrentUser.DeleteSubKeyTree(progIdKey, throwOnMissingSubKey: false);
NotifyShell();
}
internal static void AddPermanentUnlockFolderContextMenu()
{
var executablePath = GetInstalledExecutablePath();
var unlockedIconPath = GetInstalledResourcePath("Unlocked.ico");
// Register permanent unlock as a context menu entry for the ProgID (not extension)
const string progIdKey = @"Software\Classes\WinFolderLock.File";
const string extMenuKey = progIdKey + @"\shell\WinFolderLockPermUnlock";
const string extCommandKeyPath = extMenuKey + @"\command";
Registry.CurrentUser.DeleteSubKeyTree(extMenuKey, throwOnMissingSubKey: false);
using var menuKey = Registry.CurrentUser.CreateSubKey(extMenuKey);
ArgumentNullException.ThrowIfNull(menuKey);
menuKey.SetValue(string.Empty, "Permanently Unlock Folder", RegistryValueKind.String);
menuKey.SetValue("Icon", unlockedIconPath, RegistryValueKind.String);
using var commandKey = Registry.CurrentUser.CreateSubKey(extCommandKeyPath);
ArgumentNullException.ThrowIfNull(commandKey);
// Command includes a /permunlock switch to indicate permanent unlock behavior
commandKey.SetValue(string.Empty, $"\"{executablePath}\" /permunlock \"%1\"", RegistryValueKind.String);
// Refresh Explorer to show the new context menu entry immediately
NotifyShell();
}
internal static void RemovePermanentUnlockFolderContextMenu()
{
const string progIdKey = @"Software\Classes\WinFolderLock.File";
const string extKeyPath = progIdKey + @"\shell\WinFolderLockPermUnlock";
Registry.CurrentUser.DeleteSubKeyTree(extKeyPath, throwOnMissingSubKey: false);
NotifyShell();
}
internal static void InstallApplicationFiles()
{
var installDirectoryPath = GetInstallDirectoryPath();
if (Directory.Exists(installDirectoryPath))
{
Directory.Delete(installDirectoryPath, recursive: true);
}
Directory.CreateDirectory(installDirectoryPath);
var sourceDirectoryPath = GetCurrentExecutableDirectoryPath();
CopyDirectoryContents(sourceDirectoryPath, installDirectoryPath);
}
internal static void RemoveInstalledApplicationFiles()
{
var installDirectoryPath = GetInstallDirectoryPath();
if (Directory.Exists(installDirectoryPath))
{
Directory.Delete(installDirectoryPath, recursive: true);
}
}
private static string GetInstallDirectoryPath()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), ApplicationFolderName);
}
private static string GetInstalledResourcesDirectoryPath()
{
return Path.Combine(GetInstallDirectoryPath(), ResourcesFolderName);
}
internal static string GetInstalledExecutablePath()
{
var installedExecutablePath = Path.Combine(GetInstallDirectoryPath(), GetCurrentExecutableFileName());
return File.Exists(installedExecutablePath) ? installedExecutablePath : GetCurrentExecutablePath();
}
private static string GetInstalledResourcePath(string resourceFileName)
{
var resourcePath = Path.Combine(GetInstalledResourcesDirectoryPath(), resourceFileName);
if (File.Exists(resourcePath))
{
return resourcePath;
}
var execDir = GetCurrentExecutableDirectoryPath();
// First check Resources subfolder next to the executable (common layout)
var candidateInResources = Path.Combine(execDir, ResourcesFolderName, resourceFileName);
if (File.Exists(candidateInResources))
{
return candidateInResources;
}
// Next check for the file copied directly into the output directory
var candidateInOutput = Path.Combine(execDir, resourceFileName);
if (File.Exists(candidateInOutput))
{
return candidateInOutput;
}
// Fallback to the Resources subfolder path (keeps previous behavior when files are installed)
return candidateInResources;
}
private static string GetCurrentExecutablePath()
{
var executablePath = Environment.ProcessPath;
if (string.IsNullOrWhiteSpace(executablePath))
{
throw new InvalidOperationException("Could not resolve executable path for context menu registration.");
}
return executablePath;
}
private static string GetCurrentExecutableDirectoryPath()
{
var executableDirectoryPath = Path.GetDirectoryName(GetCurrentExecutablePath());
if (string.IsNullOrWhiteSpace(executableDirectoryPath))
{
throw new InvalidOperationException("Could not resolve executable directory for installation.");
}
return executableDirectoryPath;
}
private static string GetCurrentExecutableFileName()
{
return Path.GetFileName(GetCurrentExecutablePath());
}
private static void CopyDirectoryContents(string sourceDirectoryPath, string destinationDirectoryPath)
{
foreach (var filePath in Directory.EnumerateFiles(sourceDirectoryPath))
{
if (Path.GetExtension(filePath).Equals(".pdb", StringComparison.OrdinalIgnoreCase))
{
continue;
}
var destinationPath = Path.Combine(destinationDirectoryPath, Path.GetFileName(filePath));
File.Copy(filePath, destinationPath, overwrite: true);
}
foreach (var directoryPath in Directory.EnumerateDirectories(sourceDirectoryPath))
{
var destinationPath = Path.Combine(destinationDirectoryPath, Path.GetFileName(directoryPath));
Directory.CreateDirectory(destinationPath);
CopyDirectoryContents(directoryPath, destinationPath);
}
}
private static string GetContextMenuIconPath()
{
return GetInstalledResourcePath("Locked.ico");
}
}