-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
260 lines (216 loc) · 9.43 KB
/
Program.cs
File metadata and controls
260 lines (216 loc) · 9.43 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
using QuickA_Cleanup.Core.Models;
using QuickA_Cleanup.Core.Services;
namespace QuickA_Cleanup.CLI;
class Program
{
static void Main(string[] args)
{
Console.Title = "QuickA-Cleanup CLI";
Console.OutputEncoding = System.Text.Encoding.UTF8;
bool dryRun = args.Contains("--dry-run", StringComparer.OrdinalIgnoreCase);
bool noBackup = args.Contains("--no-backup", StringComparer.OrdinalIgnoreCase);
bool help = args.Contains("--help", StringComparer.OrdinalIgnoreCase)
|| args.Contains("-h", StringComparer.OrdinalIgnoreCase);
if (help) { PrintHelp(); return; }
try { RunCleanup(dryRun, !noBackup); }
catch (Exception ex)
{
Error($"\nFATAL: {ex.Message}");
Pause();
Environment.Exit(1);
}
}
static void RunCleanup(bool dryRun, bool backupEnabled)
{
Console.Clear();
PrintHeader();
if (dryRun)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine(" [DRY-RUN] No changes will be written.\n");
Console.ResetColor();
}
var scanner = new RegistryScanner();
Status("Scanning registry for Quick Access items...");
var (items, skipped) = scanner.ScanForItems();
Console.WriteLine();
if (items.Count == 0)
{
Success("No removable items found." +
(skipped > 0 ? $" ({skipped} protected item(s) hidden.)" : ""));
Pause();
return;
}
PrintTable(items, skipped);
var toRemove = GetSelection(items);
if (toRemove.Count == 0)
{
Warn("No valid selection — nothing to do.");
Pause();
return;
}
if (!Confirm(toRemove, dryRun)) { Warn("Cancelled."); return; }
if (backupEnabled && !dryRun)
{
var exeDir = AppContext.BaseDirectory;
var path = BackupService.CreateBackup(toRemove, exeDir);
if (path != null) Success($"Backup → {System.IO.Path.GetFileName(path)}");
else Warn("Backup failed — continuing anyway.");
Console.WriteLine();
}
RemoveItems(scanner, toRemove, dryRun);
if (!dryRun)
{
Console.WriteLine();
Status("Restarting Windows Explorer...");
scanner.RestartExplorer();
Success("Done! Check your File Explorer navigation pane.");
}
else
{
Success("Dry-run complete. No changes were made.");
}
Console.WriteLine();
Thread.Sleep(1500);
Pause();
}
// ── Selection ────────────────────────────────────────────────────────────
static List<QuickAccessItem> GetSelection(List<QuickAccessItem> items)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" Options:");
Console.ResetColor();
Console.WriteLine(" [number] Remove one item (e.g. 2)");
Console.WriteLine(" [n,n,n] Remove multiple items (e.g. 1,3,5)");
Console.WriteLine(" [A] Remove ALL items");
Console.WriteLine(" [Q] Quit without changes");
Console.WriteLine();
Console.Write(" Choice: ");
var input = Console.ReadLine()?.Trim() ?? "";
if (input.Equals("Q", StringComparison.OrdinalIgnoreCase))
{
Warn("Exiting without changes.");
Environment.Exit(0);
}
if (input.Equals("A", StringComparison.OrdinalIgnoreCase))
return new List<QuickAccessItem>(items);
var selected = new List<QuickAccessItem>();
foreach (var part in input.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
if (int.TryParse(part.Trim(), out int num))
{
var item = items.FirstOrDefault(i => i.Number == num);
if (item != null && !selected.Contains(item))
selected.Add(item);
}
}
if (selected.Count == 0) Warn("No matching numbers found.");
return selected;
}
static bool Confirm(List<QuickAccessItem> items, bool dryRun)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(dryRun ? " [DRY-RUN] Would remove:" : " About to remove:");
Console.ResetColor();
foreach (var i in items)
Console.WriteLine($" • {i.Name,-35} {i.Guid}");
Console.WriteLine();
Console.Write(dryRun ? " Simulate? (Y/N): " : " Confirm? (Y/N): ");
return (Console.ReadLine()?.Trim() ?? "").Equals("Y", StringComparison.OrdinalIgnoreCase);
}
static void RemoveItems(RegistryScanner scanner, List<QuickAccessItem> items, bool dryRun)
{
Console.WriteLine();
int ok = 0, fail = 0;
foreach (var item in items)
{
if (dryRun)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($" [DRY-RUN] Would remove: {item.Name}");
Console.ResetColor();
ok++;
continue;
}
if (scanner.RemoveItem(item))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" ✓ Removed : {item.Name}");
Console.ResetColor();
ok++;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($" ✗ Failed : {item.Name}");
Console.ResetColor();
fail++;
}
}
Console.WriteLine();
Console.WriteLine($" Result: {ok} succeeded, {fail} failed.");
}
// ── UI helpers ───────────────────────────────────────────────────────────
static void PrintHeader()
{
const int w = 60;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(new string('═', w));
Console.WriteLine(Center("QuickA-Cleanup CLI", w));
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(Center("Quick Access Navigation Pane Cleaner", w));
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(new string('═', w));
Console.ResetColor();
Console.WriteLine();
}
static void PrintHelp()
{
PrintHeader();
Console.WriteLine(" USAGE: QuickA-Cleanup-CLI.exe [options]\n");
Console.WriteLine(" OPTIONS:");
Console.WriteLine(" --dry-run Preview changes without writing to registry");
Console.WriteLine(" --no-backup Skip automatic .reg backup before removal");
Console.WriteLine(" --help / -h Show this help\n");
}
static void PrintTable(List<QuickAccessItem> items, int skipped)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" Found {items.Count} item(s)" +
(skipped > 0 ? $" ({skipped} protected hidden)" : "") + "\n");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($" {"#",-5} {"Name",-35} {"GUID",-40} {"Type"}");
Console.WriteLine(" " + new string('─', 88));
Console.ResetColor();
foreach (var item in items)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write($" {item.Number,-5}");
Console.ForegroundColor = item.Tags.Contains("known")
? ConsoleColor.Yellow : ConsoleColor.White;
Console.Write($"{Truncate(item.Name, 34),-35} ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"{item.Guid,-40} ");
Console.ForegroundColor = item.Tags.Contains("known")
? ConsoleColor.DarkYellow : ConsoleColor.DarkGray;
Console.WriteLine(item.Tags.Contains("known") ? "bloatware" : "unknown");
Console.ResetColor();
}
Console.WriteLine();
}
static void Status(string m) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(" ► "); Console.ResetColor(); Console.WriteLine(m); }
static void Success(string m) { Console.ForegroundColor = ConsoleColor.Green; Console.Write(" ✓ "); Console.ResetColor(); Console.WriteLine(m); }
static void Warn(string m) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(" ⚠ "); Console.ResetColor(); Console.WriteLine(m); }
static void Error(string m) { Console.ForegroundColor = ConsoleColor.Red; Console.Write(" ✗ "); Console.ResetColor(); Console.WriteLine(m); }
static void Pause() { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("\n Press Enter to exit..."); Console.ResetColor(); Console.ReadLine(); }
static string Center(string text, int width)
{
int pad = Math.Max(0, (width - text.Length) / 2);
return new string(' ', pad) + text;
}
static string Truncate(string text, int max) =>
text.Length <= max ? text : text[..(max - 1)] + "…";
}