Rory Claasen

Custom URL Protocol

2 min read

There was a request recently at work where, what if we could click a link in a Power BI report to trigger some arbitrary action in our tools.

Below is an example of how to register and then use a custom URL protocol handler in C#.

using Microsoft.Win32;
const string SchemeUrl = "myApp";
const string SchemeName = "My Application";
void RegisterUrlScheme()
var application = Environment.ProcessPath!;
using var key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Classes\\" + SchemeUrl);
key.SetValue("", "URL:" + SchemeName);
key.SetValue("URL Protocol", "");
using var defaultIcon = key.CreateSubKey("DefaultIcon");
defaultIcon.SetValue("", application + ",1");
using var command = key.CreateSubKey(@"shell\open\command");
command.SetValue("", "\"" + application + "\" \"%1\"");

Calling RegisterUrlScheme will create a registry entry that will allow us to launch the application using a custom uri scheme.

(Default) = "URL:My Application"
URL Protocol = ""
(Default) = "C:\Program Files\Example\Example.exe,1"
(Default) = "C:\Program Files\Example\Example.exe" "%1"

With something like this below, we can always ensure that the application is always registered and if launched though the uri scheme, handle it appropriately.

if (args.Length > 0)
if (Uri.TryCreate(args[0], UriKind.Absolute, out var uri)
&& string.Equals(uri.Scheme, SchemeUrl, StringComparison.OrdinalIgnoreCase))
Console.WriteLine("Just launched with the uri");

This means that we can now click on links like myApp:// and embed the url anywhere. As the result in the c# client is a Uri, we can easily parse query strings and extract any parameters we may need.