-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
154 lines (119 loc) · 5.33 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using static System.IO.Directory;
using static System.IO.File;
using Microsoft.Win32;
namespace JBRemover
{
internal static class Program
{
private static void Main()
{
Console.WriteLine(
$"Welcome to JetBrains License Remover Tool {Assembly.GetExecutingAssembly().GetName().Version}");
Start();
}
private static void Start()
{
while (true)
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var jbPath = Path.Combine(appData, "JetBrains");
if (!Directory.Exists(jbPath)) throw new PathNotFoundException("JetBrains not installed to AppData");
var projects = new[] {"DataGrip", "IntelliJIdea", "Rider", "WebStorm", "PhpStorm", "CLion"};
var availableProjects = new List<Project>()!;
foreach (var fullPath in GetDirectories(jbPath).ToList()
.Select(path => Path.Combine(jbPath, path)))
{
Debug.WriteLine(fullPath);
if (!GetAttributes(fullPath).HasFlag(FileAttributes.Directory)) continue;
availableProjects.AddRange(from project in projects
where new DirectoryInfo(fullPath).Name.ToLower().StartsWith(project.ToLower())
select new Project {Name = project, Path = fullPath});
}
switch (availableProjects.Count)
{
case 0:
throw new NullReferenceException("Not found available JetBrains projects");
case 1:
var firstProject = availableProjects.FirstOrDefault();
Console.WriteLine($"Found {firstProject?.Name}");
RemoveLicense(firstProject);
break;
case > 1:
Console.WriteLine("Please, select JetBrains project from list:");
foreach (var it in availableProjects.Select((x, i) => new {Project = x, Index = i}))
Console.WriteLine($"{it.Index + 1}. {it.Project.Name}");
short index;
do
{
Console.Write("> ");
var enter = Convert.ToChar(Console.ReadLine()?.First());
if (!char.IsNumber(enter)) continue;
index = Convert.ToInt16(char.GetNumericValue(enter));
if (index < 1 || index > availableProjects.Count) continue;
break;
} while (true);
RemoveLicense(availableProjects.ElementAt(index - 1));
break;
}
}
}
private static bool Confirmation(string message)
{
do
{
Console.Write($"{message} (Y/n): ");
var enter = Convert.ToChar(Console.ReadLine()?.ToLower().First());
if (!char.IsLetter(enter)) continue;
if (enter != 'y' && enter != 'n') continue;
return enter == 'y';
} while (true);
}
private static void RemoveLicense(Project? project)
{
if (project is null)
throw new NullReferenceException("Invalid project");
if (!Confirmation($"Remove license from {project.Name}?")) return;
if (project.Path is null) return;
foreach (var file in GetFiles(project.Path))
{
var path = Path.Combine(project.Path, file);
if (Path.GetExtension(path).ToLower() is not "key") continue;
File.Delete(path);
break;
}
var key = GetFiles(project.Path).FirstOrDefault(file => Path.GetExtension(file).ToLower() is "key");
if (!string.IsNullOrEmpty(key))
{
Console.WriteLine($"Deleting {key}...");
File.Delete(Path.Combine(project.Path, key));
}
var eval = Path.Combine(project.Path, "eval");
if( Directory.Exists(eval))
{
Console.WriteLine("Deleting eval...");
Delete(eval, true);
}
const string jbPrefPath = @"Software\JavaSoft\Prefs\jetbrains";
using var prefKey = Registry.CurrentUser.OpenSubKey(jbPrefPath, true);
if (prefKey != null && key != null)
{
Console.WriteLine($"Deleting {jbPrefPath}/{Path.GetFileNameWithoutExtension(key)}...");
prefKey.DeleteSubKeyTree(Path.GetFileNameWithoutExtension(key)!);
}
const string jbRegPath = @"Software";
using var jbKey = Registry.CurrentUser.OpenSubKey(jbRegPath, true);
if (jbKey != null)
{
Console.WriteLine($"Deleting {jbRegPath}/JetBrains...");
jbKey.DeleteSubKeyTree("JetBrains");
}
Console.WriteLine("License successfully removed!");
}
}
}