Register Custom URL Protocol handler

June 28, 2010 by C#   Integration  

In the following example we're going to create a simple url protocol (like mailto/http etc) handler, e.g.

<a href="test:123">Some Text</a>



First of all we need to create some registry entries in order to register our custom protocol, we can do this programatically like this:

RegistryKey Key = Registry.ClassesRoot.CreateSubKey("test");
Key.CreateSubKey("DefaultIcon").SetValue("", "test.exe,1");
Key.SetValue("", "test:Protocol");
Key.SetValue("URL Protocol", "");
Key.CreateSubKey(@"shell\open\command").SetValue("", "test.exe %1");

Next we're going to create a very simple console application (test.exe) handling our request, we pass values to the handler via command line - in this case the hyperlink passing the command line.

using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
        Console.ReadKey();
    }
}

Once we click on the test:123 hyperlink in the first snippet, you will notice a console application popping up - displaying the values passed to the protocol.

Note that browsers encode passed values differently, so make sure that you've tested your handler in multiple browsers.


Leave a Comment