-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
371 lines (330 loc) · 14.3 KB
/
Program.cs
File metadata and controls
371 lines (330 loc) · 14.3 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
365
366
367
368
369
370
371
/// <summary>
/// Discord Cleaner - A utility to completely remove and reinstall Discord
///
/// This tool helps resolve Discord issues by:
/// 1. Terminating all Discord processes
/// 2. Removing Discord application data
/// 3. Downloading and installing the latest Discord version
///
/// Author: DatMayo
/// Version: 0.3
/// Release: 05.10.25
/// Framework: .NET Framework 2.0 (for maximum compatibility)
/// </summary>
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
namespace DiscordCleaner
{
/// <summary>
/// Main program class containing all Discord cleaning functionality
/// </summary>
class Program
{
#region Constants
/// <summary>
/// Time to wait after killing processes before proceeding (in milliseconds)
/// </summary>
private const int PROCESS_WAIT_TIME_MS = 5000;
/// <summary>
/// Official Discord download URL for the latest Windows installer
/// </summary>
private const string DISCORD_DOWNLOAD_URL = "https://discord.com/api/downloads/distributions/app/installers/latest?channel=stable&platform=win&arch=x64";
#endregion
#region Exit Codes
/// <summary>
/// Application exit codes for different failure scenarios
/// </summary>
enum ExitCode : int
{
/// <summary>Operation completed successfully</summary>
SUCCESS = 0,
/// <summary>User chose not to continue with the operation</summary>
USER_INTERRUPT_DETECTED = 1,
/// <summary>Failed to terminate Discord main processes</summary>
COULD_NOT_TERMINATE_DISCORD_PROCESS = 2,
/// <summary>Failed to terminate Discord update processes</summary>
COULD_NOT_TERMINATE_DISCORD_UPDATE_PROCESS = 3,
/// <summary>Failed to delete Discord AppData folder</summary>
COULD_NOT_DELETE_DISCORD_APP_DATA = 4,
/// <summary>Failed to delete Discord LocalAppData folder</summary>
COULD_NOT_DELETE_DISCORD_LOCAL_APP_DATA = 5
}
#endregion
#region UI Methods
/// <summary>
/// Displays the application welcome screen with version info and warnings
/// </summary>
static void DrawStartScreen()
{
Console.Clear();
Console.WriteLine("********************************************************************************");
Console.WriteLine("* Discord Cleaner v0.3 by DatMayo Rel. 05.10.25 *");
Console.WriteLine("* WARNING! All login credentials will be lost and must be re entered! *");
Console.WriteLine("* *");
Console.WriteLine("* GitHub: https://github.com/DatMayolein/Discord-Cleaner *");
Console.WriteLine("* Icon: https://bit.ly/2HVZx0B *");
Console.WriteLine("********************************************************************************");
Console.WriteLine();
}
/// <summary>
/// Prompts the user for confirmation to proceed with Discord cleanup
/// </summary>
/// <returns>True if user confirms (Y), false otherwise. Exits application on 'N'.</returns>
static bool CleanDiscordInstallation()
{
Console.Write("Would you like to cleanup your Discord installation? (y/N) ");
ConsoleKeyInfo key = Console.ReadKey();
switch (key.Key.ToString().ToLower())
{
case "n":
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Operation aborted!");
Console.ReadKey();
Environment.Exit((int)ExitCode.USER_INTERRUPT_DETECTED);
break;
case "y":
Console.WriteLine();
Console.WriteLine();
return true;
default:
DrawStartScreen();
break;
}
return false;
}
#endregion
#region Process Management
/// <summary>
/// Terminates all Discord and Discord-Update processes
/// Includes safety checks to avoid terminating unrelated processes
/// </summary>
static void KillProcesses()
{
Console.WriteLine("Searching for Discord processes... ");
Process[] discordHandle = Process.GetProcessesByName("discord");
Console.WriteLine(string.Format("- {0} Discord process(es)", discordHandle.Length));
foreach (Process p in discordHandle)
{
try
{
Console.Write("-> Killing Discord process...");
p.Kill();
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Failed: " + ex.Message);
Console.Write("Discord process could not be terminated, aborting...");
Console.ReadKey();
Environment.Exit((int)ExitCode.COULD_NOT_TERMINATE_DISCORD_PROCESS);
}
}
Console.WriteLine("Killing Discord process finished...");
Console.WriteLine();
Console.WriteLine("Searching for Discord-Update processes... ");
// More safely identify Discord update processes
Process[] allProcesses = Process.GetProcesses();
int discordUpdateCount = 0;
foreach (Process p in allProcesses)
{
try
{
if (IsDiscordUpdateProcess(p))
{
discordUpdateCount++;
Console.Write("-> Killing Discord-Update process...");
p.Kill();
Console.WriteLine("OK");
}
}
catch (Exception ex)
{
Console.WriteLine("Failed: " + ex.Message);
Console.Write("Discord-Update process could not be terminated, aborting...");
Console.ReadKey();
Environment.Exit((int)ExitCode.COULD_NOT_TERMINATE_DISCORD_UPDATE_PROCESS);
}
}
Console.WriteLine(string.Format("- {0} Discord-Update process(es)", discordUpdateCount));
Console.WriteLine("Killing Discord-Update process finished...");
Console.WriteLine();
Console.Write("Now waiting for 5 seconds...");
Thread.Sleep(PROCESS_WAIT_TIME_MS);
Console.WriteLine("OK");
Console.WriteLine();
}
/// <summary>
/// Safely identifies if a process is a Discord update process
/// Checks process name and executable path to avoid false positives
/// </summary>
/// <param name="process">Process to examine</param>
/// <returns>True if the process is confirmed to be Discord-related</returns>
static bool IsDiscordUpdateProcess(Process process)
{
try
{
if (process.ProcessName.ToLower() == "update")
{
string fileName = process.MainModule.FileName;
return fileName.ToLower().Contains("discord") || fileName.ToLower().Contains("\\appdata\\local\\discord");
}
}
catch
{
// Access denied or process already exited
}
return false;
}
#endregion
#region File System Operations
/// <summary>
/// Removes Discord application data folders from both AppData and LocalAppData
/// This includes user settings, cached data, and installation files
/// </summary>
static void RemoveDiscord()
{
string discordAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\discord";
string discordLocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\discord";
Console.WriteLine(string.Format("Checking folder \"{0}\"", discordAppData));
if(Directory.Exists(discordAppData))
{
Console.Write("Found existing discord in appdata-folder, deleting it...");
try
{
Directory.Delete(discordAppData, true);
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Failed: " + ex.Message);
Console.Write("Discord AppData folder could not be deleted, aborting...");
Console.ReadKey();
Environment.Exit((int)ExitCode.COULD_NOT_DELETE_DISCORD_APP_DATA);
}
}
Console.WriteLine();
Console.WriteLine(string.Format("Checking folder \"{0}\"", discordLocalAppData));
if (Directory.Exists(discordLocalAppData))
{
Console.Write("Found existing discord in localappdata-folder, deleting it...");
try
{
Directory.Delete(discordLocalAppData, true);
Console.WriteLine("OK");
}
catch (Exception ex)
{
Console.WriteLine("Failed: " + ex.Message);
Console.Write("Discord LocalAppData folder could not be deleted, aborting...");
Console.ReadKey();
Environment.Exit((int)ExitCode.COULD_NOT_DELETE_DISCORD_LOCAL_APP_DATA);
}
}
Console.WriteLine();
}
#endregion
#region Download and Installation
/// <summary>
/// Downloads the latest Discord installer from official servers
/// Includes progress reporting and error handling
/// </summary>
static void DownloadDiscordSetup()
{
Console.Write("Downloading new Discord-Version (this can take some time)... ");
using (WebClient client = new WebClient())
{
// Note: .NET Framework 2.0 doesn't have Timeout property
int currentProgress = 0;
bool downloadCompleted = false;
Exception downloadError = null;
client.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
{
if(currentProgress < e.ProgressPercentage)
{
currentProgress = e.ProgressPercentage;
Console.SetCursorPosition(61, Console.CursorTop);
Console.Write(string.Format("{0}%", currentProgress));
}
};
client.DownloadFileCompleted += delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
downloadCompleted = true;
downloadError = e.Error;
if (e.Error == null)
{
Console.SetCursorPosition(60, Console.CursorTop);
Console.WriteLine("Finished!");
Console.WriteLine();
}
else
{
Console.SetCursorPosition(60, Console.CursorTop);
Console.WriteLine("Failed!");
Console.WriteLine("Error: " + e.Error.Message);
}
};
try
{
client.DownloadFileAsync(new Uri(DISCORD_DOWNLOAD_URL), "DiscordSetup.exe");
// Wait for download completion instead of infinite loop
while (!downloadCompleted)
{
Thread.Sleep(100);
}
if (downloadError != null)
{
Console.Write("Download failed, aborting...");
Console.ReadKey();
Environment.Exit(1);
}
InstallDiscord();
}
catch (Exception ex)
{
Console.WriteLine("Download failed: " + ex.Message);
Console.Write("Aborting...");
Console.ReadKey();
Environment.Exit(1);
}
}
}
/// <summary>
/// Executes the downloaded Discord installer and waits for completion
/// </summary>
static void InstallDiscord()
{
Console.Write("Starting DiscordSetup.exe...");
Process process = new Process();
process.StartInfo.FileName = "DiscordSetup.exe";
process.Start();
process.WaitForExit();// Waits here for the process to exit.
Console.WriteLine("Finished!");
Console.WriteLine();
Console.Write("All Done! Discord will start automatically.");
Console.ReadKey();
}
#endregion
#region Application Entry Point
/// <summary>
/// Application main entry point
/// Orchestrates the complete Discord cleaning and reinstallation process
/// </summary>
/// <param name="args">Command line arguments (not used)</param>
static void Main(string[] args)
{
Console.Title = "Discord Cleaner";
DrawStartScreen();
if(CleanDiscordInstallation())
{
KillProcesses();
RemoveDiscord();
DownloadDiscordSetup();
}
}
#endregion
}
}