如何在Windows 8上正确注册协议处理程序?

Der*_*ler 6 c# windows-8

我有一个小项目来处理tel:协议链接.这是一个桌面应用程序,我正在使用Visual Studio 2013 Community Edition进行开发.

以前,我曾经使用简单的注册表修改来注册处理程序:

Microsoft.Win32.Registry.SetValue(registryKey, string.Empty, registryValue, Microsoft.Win32.RegistryValueKind.String);
Microsoft.Win32.Registry.SetValue(registryKey, "URL Protocol", String.Empty, Microsoft.Win32.RegistryValueKind.String);

registryKey = @"HKEY_CLASSES_ROOT\tel\shell\open\command";
registryValue = "\"" + AppDomain.CurrentDomain.BaseDirectory + "TelProtocolHandler.exe\" \"%1\"";
Microsoft.Win32.Registry.SetValue(registryKey, string.Empty, registryValue, Microsoft.Win32.RegistryValueKind.String);
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不再适用于Windows 8.虽然注册表项具有所需的值,但链接仍由不同的应用程序处理.我的工具甚至没有出现在协议处理程序选择中:

在此输入图像描述

我查看了演练:使用Windows 8自定义协议激活,但我无法将提到的信息与我的应用程序联系起来.文章提到了一个.appxmanifest文件,我在项目中没有这个文件,无法添加为新项目.

Der*_*ler 9

在提出问题后,我偶然发现在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 );
}
Run Code Online (Sandbox Code Playgroud)

TelProtocolHandler 是我的应用程序的名称,应该由您的处理程序的名称替换.

另一个问题中接受的答案也在ApplicationDescription注册表中.我没有看到我检查过的任何其他注册处理程序的相同密钥,因此我将其删除并且无法检测到任何问题.

另一个关键问题是,如果设置处理程序的应用程序是32位,则所有这些都不起作用.当条目在Wow6432Node中生成时,我无法选择处理程序作为给定协议的默认值.我花了一段时间来解决这个问题,因为我的应用程序编译为AnyCPU.我最初错过的是项目属性中的这个小旗:

在此输入图像描述