-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEudicProtocolHandler.cs
60 lines (54 loc) · 1.69 KB
/
EudicProtocolHandler.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
using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Windows.Forms;
// Compile this code with the following command:
// csc /target:winexe EudicProtocolHandler.cs
class EudicProtocolHandler
{
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
HandleUrl(args[0]);
}
else
{
RegisterProtocol();
MessageBox.Show("Protocol registered successfully.", "Eudic Protocol Handler", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
static void RegisterProtocol()
{
string protocolName = "eudic";
string applicationPath = Application.ExecutablePath;
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(protocolName))
{
string commandKey = $@"{protocolName}\shell\open\command";
Registry.ClassesRoot.CreateSubKey(commandKey).SetValue("",
$"\"{applicationPath}\" \"%1\"", RegistryValueKind.ExpandString);
key.SetValue("", $"URL:{protocolName} Protocol");
key.SetValue("URL Protocol", "");
}
}
static void HandleUrl(string url)
{
try
{
Uri uri = new Uri(url);
string[] segments = uri.Segments;
if (segments.Length > 1)
{
string word = segments[segments.Length - 1].TrimEnd('/');
string eudicPath = @"C:\Program Files\eudic\eudic.exe";
string arguments = $"-w \"{word}\"";
Process.Start(eudicPath, arguments);
}
}
catch (Exception)
{
// Silently ignore invalid URL
}
}
}