-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
137 lines (121 loc) · 5.08 KB
/
Utils.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
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace yt_dl_protocol
{
public static class Utils
{
public static bool ValidateExecutableIsYtDl(string executable)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = executable,
Arguments = $" --version",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process { StartInfo = startInfo })
{
process.Start();
string output = process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd();
process.WaitForExit();
return Regex.IsMatch(output, @"\b\d{4}\.\d{2}\.\d{2}\b");
}
}
public static bool RegisterURLProtocol(string protocol, string applicationPath, string description, bool silent= true)
{
try
{
RegistryKey key = Registry.ClassesRoot.CreateSubKey(protocol);
key.SetValue(string.Empty, "URL:" + description);
key.SetValue("URL Protocol", string.Empty);
using (RegistryKey defaultIcon = key.CreateSubKey("DefaultIcon"))
{
defaultIcon.SetValue(string.Empty, applicationPath + ",1");
}
using (RegistryKey shellKey = key.CreateSubKey(@"shell\open\command"))
{
shellKey.SetValue(string.Empty, $"\"{applicationPath}\" \"%1\"");
}
if (silent)
MessageBox.Show("Protocol registered successfully. Do not rename this executable or move it to another location.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
if (silent)
MessageBox.Show($"Failed to register protocol: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
public static bool IsProtocolRegistered(string protocol)
{
string applicationPath = Assembly.GetExecutingAssembly().Location;
try
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(protocol))
{
if (key == null)
{
return false;
}
using (RegistryKey shellKey = key.OpenSubKey(@"shell\open\command"))
{
if (shellKey == null)
{
return false;
}
string registeredApplicationPath = shellKey.GetValue(string.Empty) as string;
if (registeredApplicationPath == null)
{
return false;
}
registeredApplicationPath = registeredApplicationPath.Replace("\"%1\"", "").Trim('\"', ' ');
return string.Equals(registeredApplicationPath, applicationPath, StringComparison.OrdinalIgnoreCase);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to check protocol registration: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public static bool RemoveOrphanedKeys(string protocol)
{
try
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(protocol))
{
if (key == null)
{
return true;
}
}
Registry.ClassesRoot.DeleteSubKeyTree(protocol);
return true;
}
catch (Exception)
{
return false;
}
}
public static bool UnregisterURLProtocol(string protocol, bool silent = true)
{
Registry.ClassesRoot.DeleteSubKeyTree(protocol, false);
bool cleanedOrphanedKeys = RemoveOrphanedKeys(protocol);
if (cleanedOrphanedKeys)
if (silent)
MessageBox.Show("The protocol has been unregistered. To re-register, run the program again to open the configuration menu.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
if (silent)
MessageBox.Show("The protocol has been unregistered, but some orphaned registry keys may not be removed.\n\nTo re-register, run the program again to open the configuration menu.", "Success with warnings", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
}