Skip to content

Commit ffdf68e

Browse files
author
Franze, Mario
committed
+ Added download progess to console window
+ Added exit codes * Renamed GetProcesses() into KillProcesses * Split drawing & installation routine into 2 functions - removed unused using directives
1 parent 8be09fc commit ffdf68e

File tree

1 file changed

+109
-101
lines changed

1 file changed

+109
-101
lines changed

Program.cs

Lines changed: 109 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,66 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Net;
5-
using System.Security.Permissions;
65

76
namespace DiscordCleaner
87
{
98
class Program
109
{
10+
enum ExitCode : int
11+
{
12+
SUCCESS = 0,
13+
USER_INTERRUPT_DETECTED = 1,
14+
COULD_NOT_TERMINATE_DISCORD_PROCESS = 2,
15+
COULD_NOT_TERMINATE_DISCORD_UPDATE_PROCESS = 3,
16+
COULD_NOT_DELETE_DISCORD_APP_DATA = 4,
17+
COULD_NOT_DELETE_DISCORD_LOCAL_APP_DATA = 5
18+
}
19+
1120
static void DrawStartScreen()
1221
{
1322
Console.Clear();
1423
Console.WriteLine("********************************************************************************");
15-
Console.WriteLine("* Discord Cleaner v0.1 by DatMayo Rel. 01.05.18 *");
24+
Console.WriteLine("* Discord Cleaner v0.2 by DatMayo Rel. 07.05.18 *");
1625
Console.WriteLine("* WARNING! All login credentials will be lost and must be re entered! *");
1726
Console.WriteLine("* *");
1827
Console.WriteLine("* GitHub: https://github.com/DatMayolein/Discord-Cleaner *");
28+
Console.WriteLine("* Icon: https://bit.ly/2HVZx0B *");
1929
Console.WriteLine("********************************************************************************");
20-
Console.WriteLine("");
30+
Console.WriteLine();
31+
}
32+
33+
static bool CleanDiscordInstallation()
34+
{
2135
Console.Write("Would you like to cleanup your Discord installation? (y/N) ");
2236
ConsoleKeyInfo key = Console.ReadKey();
23-
if (key.Key.ToString().ToLower() == "n")
37+
switch (key.Key.ToString().ToLower())
2438
{
25-
Console.WriteLine("");
26-
Console.WriteLine("");
27-
Console.WriteLine("Operation aborted!");
28-
Console.ReadKey();
39+
case "n":
40+
Console.WriteLine();
41+
Console.WriteLine();
42+
Console.WriteLine("Operation aborted!");
43+
Console.ReadKey();
44+
Environment.Exit((int)ExitCode.USER_INTERRUPT_DETECTED);
45+
break;
46+
47+
case "y":
48+
Console.WriteLine();
49+
Console.WriteLine();
50+
return true;
51+
52+
default:
53+
DrawStartScreen();
54+
break;
2955
}
30-
else if (key.Key.ToString().ToLower() == "y")
31-
GetProcesses();
32-
else
33-
DrawStartScreen();
56+
return false;
3457
}
3558

36-
static void GetProcesses()
59+
static void KillProcesses()
3760
{
38-
Console.WriteLine("");
39-
bool killFailed = false;
40-
Console.WriteLine("");
4161
Console.WriteLine("Searching for Discord processes... ");
4262
Process[] discordHandle = Process.GetProcessesByName("discord");
4363
Console.WriteLine(string.Format("- {0} Discord process(es)", discordHandle.Length));
44-
foreach(Process p in discordHandle)
64+
foreach (Process p in discordHandle)
4565
{
4666
try
4767
{
@@ -52,56 +72,43 @@ static void GetProcesses()
5272
catch
5373
{
5474
Console.WriteLine("Failed");
55-
killFailed = true;
75+
Console.Write("Process could not be killed, aborting...");
76+
Console.ReadKey();
77+
Environment.Exit((int)ExitCode.COULD_NOT_TERMINATE_DISCORD_PROCESS);
5678
}
5779
}
58-
if (killFailed)
59-
{
60-
Console.WriteLine("Can not proceed, some discord processes are still running!");
61-
Console.ReadKey();
62-
}
63-
else
80+
81+
Console.WriteLine("Killing Discord process finished...");
82+
Console.WriteLine();
83+
Console.WriteLine("Searching for Discord-Update processes... ");
84+
Process[] updateHandle = Process.GetProcessesByName("update");
85+
Console.WriteLine(string.Format("- {0} Discord-Update process(es)", updateHandle.Length));
86+
foreach (Process p in updateHandle)
6487
{
65-
Console.WriteLine("Killing Discord process finished...");
66-
Console.WriteLine("");
67-
Console.WriteLine("Searching for Discord-Update processes... ");
68-
Process[] updateHandle = Process.GetProcessesByName("update");
69-
Console.WriteLine(string.Format("- {0} Discord-Update process(es)", updateHandle.Length));
70-
foreach (Process p in updateHandle)
88+
try
7189
{
72-
try
73-
{
74-
Console.Write("-> Killing Discord process...");
75-
p.Kill();
76-
Console.WriteLine("OK");
77-
}
78-
catch
79-
{
80-
Console.WriteLine("Failed");
81-
killFailed = true;
82-
}
90+
Console.Write("-> Killing Discord process...");
91+
p.Kill();
92+
Console.WriteLine("OK");
8393
}
84-
if (killFailed)
94+
catch
8595
{
86-
Console.WriteLine("Can not proceed, some discord processes are still running!");
96+
Console.WriteLine("Failed");
97+
Console.Write("Process could not be killed, aborting...");
8798
Console.ReadKey();
88-
}
89-
else
90-
{
91-
Console.WriteLine("Killing Discord-Update process finished...");
92-
Console.WriteLine("");
93-
Console.Write("Now waiting for 5 seconds...");
94-
System.Threading.Thread.Sleep(5000); //Necessary, for whatever reason
95-
Console.WriteLine("OK");
96-
Console.WriteLine("");
97-
RemoveDiscord();
99+
Environment.Exit((int)ExitCode.COULD_NOT_TERMINATE_DISCORD_PROCESS);
98100
}
99101
}
102+
Console.WriteLine("Killing Discord-Update process finished...");
103+
Console.WriteLine();
104+
Console.Write("Now waiting for 5 seconds...");
105+
System.Threading.Thread.Sleep(5000); //Necessary, for whatever reason
106+
Console.WriteLine("OK");
107+
Console.WriteLine();
100108
}
101109

102110
static void RemoveDiscord()
103111
{
104-
bool deleteFailed = false;
105112
string discordAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\discord";
106113
string discordLocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\discord";
107114
Console.WriteLine(string.Format("Checking folder \"{0}\"", discordAppData));
@@ -113,59 +120,60 @@ static void RemoveDiscord()
113120
Directory.Delete(discordAppData, true);
114121
Console.WriteLine("OK");
115122
}
116-
catch (Exception ex)
123+
catch
117124
{
118-
deleteFailed = true;
119125
Console.WriteLine("Failed");
120-
Console.Write(ex.Message);
126+
Console.Write("Process could not be killed, aborting...");
127+
Console.ReadKey();
128+
Environment.Exit((int)ExitCode.COULD_NOT_DELETE_DISCORD_APP_DATA);
121129
}
122130
}
123-
if (deleteFailed)
124-
{
125-
Console.WriteLine("Can not proceed, some discord files may remain!");
126-
Console.ReadKey();
127-
}
128-
else
131+
132+
Console.WriteLine();
133+
Console.WriteLine(string.Format("Checking folder \"{0}\"", discordLocalAppData));
134+
if (Directory.Exists(discordLocalAppData))
129135
{
130-
Console.WriteLine("");
131-
Console.WriteLine(string.Format("Checking folder \"{0}\"", discordLocalAppData));
132-
if (Directory.Exists(discordLocalAppData))
136+
Console.Write("Found existing discord in localappdata-folder, deleting it...");
137+
try
133138
{
134-
Console.Write("Found existing discord in localappdata-folder, deleting it...");
135-
try
136-
{
137-
Directory.Delete(discordLocalAppData, true);
138-
Console.WriteLine("OK");
139-
}
140-
catch (Exception ex)
141-
{
142-
deleteFailed = true;
143-
Console.WriteLine("Failed");
144-
Console.Write(ex.Message);
145-
}
139+
Directory.Delete(discordLocalAppData, true);
140+
Console.WriteLine("OK");
146141
}
147-
if (deleteFailed)
142+
catch
148143
{
149-
Console.WriteLine("Can not proceed, some discord files may remain!");
144+
Console.WriteLine("Failed");
145+
Console.Write("Process could not be killed, aborting...");
150146
Console.ReadKey();
151-
}
152-
else
153-
{
154-
Console.WriteLine("");
155-
DownloadDiscordSetup();
147+
Environment.Exit((int)ExitCode.COULD_NOT_DELETE_DISCORD_LOCAL_APP_DATA);
156148
}
157149
}
150+
Console.WriteLine();
158151
}
159152

160153
static void DownloadDiscordSetup()
161154
{
162-
Console.Write("Downloading new Discord-Version (this can take some time)...");
155+
Console.Write("Downloading new Discord-Version (this can take some time)... ");
163156
WebClient client = new WebClient();
164-
byte[] DiscordSetup = client.DownloadData("https://discordapp.com/api/download?platform=win");
165-
File.WriteAllBytes("DiscordSetup.exe", DiscordSetup);
166-
Console.WriteLine("Finished");
167-
Console.WriteLine("");
168-
InstallDiscord();
157+
int currentProgress = 0;
158+
client.DownloadProgressChanged += (s, e) =>
159+
{
160+
if(currentProgress < e.ProgressPercentage)
161+
{
162+
currentProgress = e.ProgressPercentage;
163+
Console.SetCursorPosition(61, Console.CursorTop);
164+
Console.Write(string.Format("{0}%", currentProgress));
165+
}
166+
};
167+
client.DownloadFileCompleted += (s, e) =>
168+
{
169+
Console.SetCursorPosition(60, Console.CursorTop);
170+
Console.WriteLine("Finished!");
171+
Console.WriteLine();
172+
InstallDiscord();
173+
};
174+
client.DownloadFileAsync(new Uri("https://discordapp.com/api/download?platform=win"), "DiscordSetup.exe");
175+
while(true)
176+
Console.ReadKey();
169177
}
170178

171179
static void InstallDiscord()
@@ -175,23 +183,23 @@ static void InstallDiscord()
175183
process.StartInfo.FileName = "DiscordSetup.exe";
176184
process.Start();
177185
process.WaitForExit();// Waits here for the process to exit.
178-
Console.WriteLine("Finished");
179-
Console.WriteLine("");
180-
Console.WriteLine("All Done! Discord will start automatically.");
181-
Console.ReadKey();
182-
}
183-
184-
static void DownloadFinished()
185-
{
186-
//Console
186+
Console.WriteLine("Finished!");
187+
Console.WriteLine();
188+
Console.Write("All Done! Discord will start automatically.");
187189
Console.ReadKey();
188190
}
189191

190192
static void Main(string[] args)
191193
{
192194
Console.Title = "Discord Cleaner";
193-
Console.SetWindowSize(80, 40);
194195
DrawStartScreen();
196+
if(CleanDiscordInstallation())
197+
{
198+
KillProcesses();
199+
RemoveDiscord();
200+
DownloadDiscordSetup();
201+
202+
}
195203
}
196204
}
197205
}

0 commit comments

Comments
 (0)