forked from richard-green/MstscLauncher
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
134 lines (114 loc) · 4.78 KB
/
Program.cs
File metadata and controls
134 lines (114 loc) · 4.78 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
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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Windows.Forms;
namespace MstscLauncher
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
var mstsc = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\mstsc.exe");
if (File.Exists(mstsc) == false)
{
MessageBox.Show(String.Format("{0} was not found", mstsc), "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SetupRegistry(mstsc);
if (args.Length == 0)
{
return;
}
Uri uri = new Uri(args[0]);
if (uri.Scheme.Equals("mstsc", StringComparison.CurrentCultureIgnoreCase))
{
var host = String.Format("/v:{0}{1}", uri.Host, uri.IsDefaultPort ? "" : String.Format(":{0}", uri.Port));
if (String.IsNullOrEmpty(uri.Query) == false)
{
var arguments = new List<string>() { host };
var query = HttpUtility.ParseQueryString(uri.Query);
foreach (var key in query.AllKeys)
{
switch (key)
{
case "admin":
case "f":
case "public":
case "span":
case "multimon":
case "restrictedAdmin":
case "remoteGuard":
case "prompt":
case "control":
case "noConsentPrompt":
arguments.Add(String.Format("/{0}", key));
break;
case "w":
case "h":
case "shadow":
arguments.Add(String.Format("/{0}:{1}", key, query[key]));
break;
}
}
Execute(mstsc, arguments);
}
else
{
Execute(mstsc, host);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Mstsc Launcher - Fatal error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
static void Execute(string exe, params string[] args)
{
Execute(exe, args.AsEnumerable());
}
static void Execute(string exe, IEnumerable<string> args)
{
var path = new FileInfo(exe);
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = path.Name;
p.StartInfo.WorkingDirectory = path.Directory.FullName;
p.StartInfo.Arguments = String.Join(" ", args.Where(s => String.IsNullOrEmpty(s) == false));
p.Start();
}
static void SetupRegistry(string exe)
{
var launcher = Path.Combine(Environment.CurrentDirectory, "MstscLauncher.exe");
var hkcr = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default);
var mstsc = hkcr.OpenSubKey("mstsc");
if (mstsc == null)
{
try
{
mstsc = hkcr.CreateSubKey("mstsc");
var DefaultIcon = mstsc.CreateSubKey("DefaultIcon");
var Shell = mstsc.CreateSubKey("Shell");
var Open = Shell.CreateSubKey("Open");
var Command = Open.CreateSubKey("Command");
mstsc.SetValue("", "URL:Remote Desktop Client Launcher");
mstsc.SetValue("URL Protocol", "");
DefaultIcon.SetValue("", String.Format("\"{0}\",1", launcher));
Command.SetValue("", String.Format("\"{0}\" \"%1\"", launcher));
MessageBox.Show("URL handler registered", "Mstsc Launcher", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Could not register as URL handler, please run again as administrator", "Mstsc Launcher", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}