C# - Register user defined URL protocol

By , 8/17/2013
(0 ratings)
Everyone knows HTTP-URLs. Windows Shell also enables to define own URL protocols. Some programs (like Visual Studio Help ms-help:// ... or Steam steam:// ...) take advantage of this feature. By creating some registry entries one is able to set up a self-made URL protocol. This allows to access your applications by URL (originating from every software).

Please notice, that the application has to have admin privileges to be able to write the needed stuff into registry. You can test your application very easy by opening Windows Explorer and typing "yoururlprotocol://testdata" into the path/address field.

Author: Rainbird, translation by Michael List
// using Microsoft.Win32; do not forget!

/// <summary>
/// Registers an user defined URL protocol for the usage with
/// the Windows Shell, the Internet Explorer and Office.
/// 
/// Example for an URL of an user defined URL protocol:
/// 
///   rainbird://RemoteControl/OpenFridge/GetBeer
/// </summary>
/// <param name="protocolName">Name of the protocol (e.g. "rainbird" für "rainbird://...")</param>
/// <param name="applicationPath">Complete file system path to the EXE file, which processes the URL being called (the complete URL is handed over as a Command Line Parameter).</param>
/// <param name="description">Description (e.g. "URL:Rainbird Custom URL")</param>
public void RegisterURLProtocol(string protocolName, string applicationPath, string description)
{
    // Create new key for desired URL protocol
    RegistryKey myKey=Registry.ClassesRoot.CreateSubKey(protocolName);

    // Assign protocol
    myKey.SetValue(null, description);
    myKey.SetValue("URL Protocol", string.Empty);

    // Register Shell values
    Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell");
    Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open");
    myKey = Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open\\command");

    // Specify application handling the URL protocol
    myKey.SetValue(null, "\"" + applicationPath,  + "\" %1");
}

Comments

 

Log in, to comment!