问完这个问题后,我偶然发现了在 Windows 8 中注册协议处理程序
尽管还有其他问题,但投票率最高的答案让我走上了正确的道路。最后,这就是我的结果:
// Register as the default handler for the tel: protocol.
const string protocolValue = "TEL:Telephone Invocation";
Registry.SetValue(
@"HKEY_CLASSES_ROOT\tel",
string.Empty,
protocolValue,
RegistryValueKind.String );
Registry.SetValue(
@"HKEY_CLASSES_ROOT\tel",
"URL Protocol",
String.Empty,
RegistryValueKind.String );
const string binaryName = "tel.exe";
string command = string.Format( "\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, binaryName );
Registry.SetValue( @"HKEY_CLASSES_ROOT\tel\shell\open\command", string.Empty, command, RegistryValueKind.String );
// For Windows 8+, register as a choosable protocol handler.
// Version detection from https://stackoverflow.com/a/17796139/259953
Version win8Version = new Version( 6, 2, 9200, 0 );
if( Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8Version ) {
Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\TelProtocolHandler",
string.Empty,
protocolValue,
RegistryValueKind.String );
Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\TelProtocolHandler\shell\open\command",
string.Empty,
command,
RegistryValueKind.String );
Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\TelProtocolHandler\Capabilities\URLAssociations",
"tel",
"TelProtocolHandler",
RegistryValueKind.String );
Registry.SetValue(
@"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
"TelProtocolHandler",
@"SOFTWARE\TelProtocolHandler\Capabilities",
RegistryValueKind.String );
}
TelProtocolHandler是我的应用程序的名称,应替换为您的处理程序的名称。
另一个问题中接受的答案也存在ApplicationDescription于注册表中。对于我检查过的任何其他注册处理程序,我都没有看到相同的密钥,所以我把它排除在外,无法检测到任何问题。
另一个关键问题是,如果我设置处理程序的应用程序是 32 位的,那么所有这些都不起作用。在 Wow6432Node 中创建条目时,我无法选择处理程序作为给定协议的默认值。我花了一段时间才弄清楚这一点,因为我的应用程序被编译为 AnyCPU。我首先错过的是项目属性中的这个小标志:
