c# - 如何在 Windows 8 上正确注册协议(protocol)处理程序?

标签 c# windows-8 protocol-handler

我有一个 small project to handle tel: protocol links .这是一个桌面应用程序,我正在使用 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);

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

enter image description here

我看过Walkthrough: Using Windows 8 Custom Protocol Activation ,但我无法将提到的信息与我的申请相关联。文章提到了一个 .appxmanifest 文件,我的项目中没有该文件,因此无法添加为新项目。

最佳答案

问完问题后,我偶然发现了Registering a protocol handler in 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。对于我检查过的任何其他已注册处理程序,我都没有看到相同的 key ,因此我将其遗漏并且无法检测到任何问题。

另一个关键问题是,如果我设置处理程序的应用程序是 32 位的,那么所有这一切都将不起作用。当在 Wow6432Node 中创建条目时,我无法选择处理程序作为给定协议(protocol)的默认处理程序。我花了一段时间才弄清楚这一点,因为我的应用程序被编译为 AnyCPU。我首先错过的是项目属性中的这个小标志:

enter image description here

关于c# - 如何在 Windows 8 上正确注册协议(protocol)处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248542/

相关文章:

c# - 在自定义构建任务中运行 Project.Build() 时如何获取构建输出、错误列表?

windows-8 - 启用/禁用 Microsoft 虚拟 WiFi 微型端口

java - 编写可移植到 Firefox、Chrome 和 IE 上运行的自定义协议(protocol)处理程序

c# - Visual Studio 协议(protocol)处理程序 - 打开文件

c# - 发送数据时,为什么 System.Net.Http.StreamContent 将整个流读入内存,忽略给定的缓冲区大小?

C#:继承和 IDisposable - 奇怪的问题

c# - 启用跨域 ASP.net Web API

mvvm - Caliburn Micro 和跨应用程序共享 View 模型

c# - 如何将文件从 Visual Studio 部署到 XAML Metro 应用程序中的可访问文件夹?

javascript - 如何检测浏览器的协议(protocol)处理程序?