Skip to content

Instantly share code, notes, and snippets.

@vbfox
Last active August 29, 2015 14:17
Show Gist options
  • Save vbfox/bb89ce344a83d541947e to your computer and use it in GitHub Desktop.
Save vbfox/bb89ce344a83d541947e to your computer and use it in GitHub Desktop.
Check if URL prototocol is handled on windows
using System;
using System.IO;
using System.Security;
using Microsoft.Win32;
static class UrlProtocol
{
public static bool IsSupported(string protocol)
{
if (protocol == null) throw new ArgumentNullException("protocol");
RegistryKey rootKey = null;
RegistryKey commandKey = null;
try
{
rootKey = Registry.ClassesRoot.OpenSubKey(protocol);
if (rootKey == null) return false;
if (rootKey.GetValue("URL Protocol") == null)
{
return false;
}
commandKey = rootKey.OpenSubKey(@"shell\open\command");
if (commandKey == null) return false;
return commandKey.GetValue(null) != null;
}
catch (SecurityException)
{
return false;
}
catch (IOException)
{
return false;
}
catch (UnauthorizedAccessException)
{
return false;
}
finally
{
if (rootKey != null) rootKey.Close();
if (commandKey != null) commandKey.Close();
}
}
}
/* USAGE (LINQPad) :
UrlProtocol.IsSupported("callto").Dump();
UrlProtocol.IsSupported("tel").Dump();
UrlProtocol.IsSupported("http").Dump();
UrlProtocol.IsSupported(".doc").Dump();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment