Register Custom URL Protocol handler



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.







Post comment

Name *
Email
Title
Body *
Security Code
*
* Required fields

Latest Posts

Be the best stalker you can be


2011-12-13 22:33:54

Syntactic sugar (C#): Enum


2011-08-04 16:50:18

Top 5 posts

Simple WYSIWYG Editor


Creating a WYSIWYG textbox for your website is actually quite simple.
2007-02-01 12:00:00

Moving items between listboxes in ASP.net/PHP example


Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
2008-06-12 17:07:43

Cross Browser Issues: Firefox Word Wrapping


Firefox word wrapping issues
2008-06-09 09:51:21

Populate a TreeView Control C#


Populate a TreeView control in a windows application.
2009-08-27 16:01:03

C# YouTube : Google API


Post on how to integrate with YouTube using the Google Data API
2011-03-12 08:37:51