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; } } } }