Last active
August 29, 2015 14:17
-
-
Save vbfox/bb89ce344a83d541947e to your computer and use it in GitHub Desktop.
Check if URL prototocol is handled on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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