-
Notifications
You must be signed in to change notification settings - Fork 24
/
Form1.cs
157 lines (131 loc) · 3.64 KB
/
Form1.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
155
156
157
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
namespace HFlashPlayer
{
public partial class HFlashPlayer : Form
{
static string Scheme { get; set; } = "hflash";
public string RegisteredScheme { get; set; }
public string RegisteredApplication { get; set; }
public string RegisterApplication { get { return Application.ExecutablePath + " %1"; } }
public HFlashPlayer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var args = Environment.GetCommandLineArgs();
var cmd = args.Length > 1 ? args[1] : null;
if (String.IsNullOrWhiteSpace(cmd))
{
CheckRegistry();
if (String.IsNullOrEmpty(RegisteredScheme))
{
AdminRun("--register");
Application.Exit();
}
else if (RegisterApplication != RegisteredApplication)
{
AdminRun("--register");
Application.Exit();
}
this.lblInfo.Text = $"Scheme {RegisteredScheme} registered to {RegisteredApplication}";
}
else
{
if (cmd.Length > Scheme.Length + 3 && cmd.Substring(0, Scheme.Length + 3) == Scheme + "://")
{
cmd = cmd.Substring(Scheme.Length + 3);
}
if (cmd.Substring(cmd.Length - 1) == "/") cmd = cmd.Substring(0, cmd.Length - 1);
if (cmd == "--register")
{
Register();
Application.Exit();
}
else
{
this.lblInfo.Text = $"cmd:"+cmd;
var url = Base64Decode(cmd);
this.lblInfo.Text = $"url:"+url;
Open(url);
Application.Exit();
}
}
//Application.Exit();
}
private void CheckRegistry()
{
var key = Registry.ClassesRoot.OpenSubKey(Scheme, false);
if (key != null)
{
RegisteredScheme = Scheme;
}
var key2 = Registry.ClassesRoot.OpenSubKey(Scheme + "\\shell\\open\\command\\", false);
if (key2 != null)
{
RegisteredApplication = (string)key2.GetValue("");
}
}
private void Register()
{
var key = Registry.ClassesRoot.OpenSubKey(Scheme, true);
if (key == null)
{
key = Registry.ClassesRoot.CreateSubKey(Scheme);
key.SetValue("URL Protocol", "");
key.SetValue("", $"URL:{Scheme} Protocol");
var iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Application.ExecutablePath + ",1");
var key2 = key.CreateSubKey("shell");
var key3 = key2.CreateSubKey("open");
var key4 = key3.CreateSubKey("command");
key4.SetValue("", RegisterApplication);
}
else
{
var appkey = key.OpenSubKey("shell\\open\\command\\", true);
if (appkey != null)
{
appkey.SetValue("", RegisterApplication);
}
}
MessageBox.Show("Scheme " + Scheme + " Registered to " + Application.ExecutablePath);
}
private void AdminRun(string cmd)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = true;
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = Application.ExecutablePath;
proc.Arguments = cmd;
proc.Verb = "runas";
try
{
Process.Start(proc);
}
catch
{
return;
}
}
private string Base64Decode(string s)
{
var data = Convert.FromBase64String(s);
return System.Text.Encoding.UTF8.GetString(data);
}
private void Open(string url)
{
var p = Process.Start(System.IO.Path.Combine(Application.StartupPath, "flashplayer.exe"), url);
p.WaitForInputIdle();
}
}
}