URL-Protocol-output

Register custom URL Protocol using C# – Windows Application

Register custom URL Protocol using C# – Windows Application

Hello folks, today we will understand what is Custom URL protocol and how can we register custom URL protocol using C#. Below is code snippet to register custom URL protocol using C#.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Custom_URL_Protocol
{
    class Program
    {
        static void Main(string[] args)
        {
            RegisterURLProtocol("TechoThirsty", @"C:\Windows\notepad.exe");
        }

        // using Microsoft.Win32; do not forget!
        /// </summary>
        /// <param name="protocolName">Name of the protocol (e.g. "technothirsty"")</param>
        /// <param name="applicationPath">Complete file system path to the EXE file, which processes the URL being called.</param>
        public static void RegisterURLProtocol(string protocolName, string applicationPath)
        {
            try
            {
                // Create new key for desired URL protocol

                var KeyTest = Registry.CurrentUser.OpenSubKey("Software", true).OpenSubKey("Classes", true);
                RegistryKey key = KeyTest.CreateSubKey(protocolName);
                key.SetValue("URL Protocol", protocolName);
                key.CreateSubKey(@"shell\open\command").SetValue("", "\"" + applicationPath + "\"");
                //key.CreateSubKey(@"shell\open\command").SetValue("", "\"" + applicationPath + "\" \"%1\"");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
    }
}

 

As shown in code, let me explain first of all open sub key of Software and register(create) new key. You can see in below image we have registered “TechnoThirsty” protocol in Registry.

Registry-entry-for-custom-url-protocol

 

Now below is the code snippet of HTML page to run our registered custom URL protocol

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Launch Technothirsty Custom URL </title>
    <script type="text/javascript">
        function LaunchURLScript() {
            var url = "TechoThirsty:";
            window.open(url);
            self.focus();
        }
    </script>
</head>
<body style="background-color:#D7D7D7">
    <input type="submit" name="Launch" id="Launch" value="Launch Technothirsty Custom URL" onclick="LaunchURLScript()">
</body>
</html>

Output:

URL-Protocol-output
URL Protocol output for sample application

 

Leave a Reply