Repository navigation
Expand file tree
/
Copy pathSetup.cs
More file actions
82 lines (75 loc) · 2.57 KB
/
Copy pathSetup.cs
File metadata and controls
82 lines (75 loc) · 2.57 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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security.Principal;
using Microsoft.Win32;
namespace Launcher
{
internal static class Setup
{
public static string ProcessFilename
{
get { return Process.GetCurrentProcess().MainModule.FileName; }
}
private static bool HasAdministrativeRight()
{
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
return windowsIdentity != null
&& new WindowsPrincipal(windowsIdentity).IsInRole(WindowsBuiltInRole.Administrator);
}
private static void RestartElevated(string command)
{
var processInfo = new ProcessStartInfo
{
Verb = "runas",
FileName = ProcessFilename,
Arguments = command
};
try
{
Process.Start(processInfo);
}
catch (Win32Exception)
{
//Do nothing. Probably the user canceled the UAC window
}
}
private static void RegisterProtocol(string protocol)
{
string key = @"HKEY_CLASSES_ROOT\" + protocol;
Registry.SetValue(key, "", "URL:Launch browser for external link");
Registry.SetValue(key, "URL Protocol", "");
Registry.SetValue(key + @"\DefaultIcon", "URL Protocol", ProcessFilename);
Registry.SetValue(key + @"\shell\open\command", "", string.Format(@"""{0}"" open ""%1""", ProcessFilename));
}
internal static bool IsInstalled(string protocol)
{
string key = @"HKEY_CLASSES_ROOT\" + protocol;
return string.Equals(ProcessFilename,
Registry.GetValue(key + @"\DefaultIcon", "URL Protocol", null) as string,
StringComparison.OrdinalIgnoreCase);
}
internal static bool Install()
{
if (HasAdministrativeRight())
{
RegisterProtocol("extern");
RegisterProtocol("externs");
return true;
}
RestartElevated("install");
return false;
}
internal static bool Uninstall()
{
if (HasAdministrativeRight())
{
Registry.ClassesRoot.DeleteSubKeyTree("extern", false);
Registry.ClassesRoot.DeleteSubKeyTree("externs", false);
return true;
}
RestartElevated("uninstall");
return false;
}
}
}