123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using Microsoft.Win32;
- using System;
- namespace CustomUrlSchemeRegistrationPoC
- {
- internal class CustomUriSchemeRegistration
- {
- private readonly string _urlScheme;
- private readonly string _displayName;
- private readonly string _exeFilePath;
- public CustomUriSchemeRegistration(string urlScheme, string displayName, string exeFilePath)
- {
- _urlScheme = urlScheme;
- _displayName = displayName;
- _exeFilePath = exeFilePath;
- }
- private static RegistryKey Target => Registry.ClassesRoot;
- public bool Install(out Exception exception)
- {
- try
- {
- using (var keyClass = Target.CreateSubKey(_urlScheme))
- {
- keyClass.SetValue(null, _displayName);
- keyClass.SetValue("URL Protocol", "");
- }
- using (var keyCommand = Target.CreateSubKey(_urlScheme + @"\shell\open\command"))
- {
- keyCommand.SetValue(null, "\"" + _exeFilePath + "\" %1");
- }
- exception = null;
- return true;
- }
- catch (Exception ex)
- {
- exception = ex;
- return false;
- }
- }
- public bool Uninstall(out Exception exception)
- {
- try
- {
- using (Target)
- {
- if (Target.OpenSubKey(_urlScheme) != null)
- {
- Target.DeleteSubKeyTree(_urlScheme);
- }
- }
- exception = null;
- return true;
- }
- catch (Exception ex)
- {
- exception = ex;
- return false;
- }
- }
- }
- }
|