2014-12-02 87 views
5

我有一个small project to handle tel: protocol links。这是一个桌面应用程序,我正在使用Visual Studio 2013 Community Edition进行开发。如何在Windows 8上正确注册协议处理程序?

以前,我用一个简单的注册表修改注册的处理程序:

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的工作,而该注册表项具有所需的值,该环节还由不同的应用程序处理。我的工具甚至没有出现在协议处理程序的选择:

enter image description here

我已经看了Walkthrough: Using Windows 8 Custom Protocol Activation,但我可以不涉及所提到的信息,我的应用程序。文章提到了一个.appxmanifest文件,我没有在我的项目中,并且无法添加为新项目。

+0

是您的应用程序在Windows商店应用程序或桌面应用程序?你正在使用哪个Visual Studio版本? |链接文章的哪一部分有问题?您应该可以在项目设置的协议列表中添加'tel:',然后它将显示在用户可以选择协议处理程序的列表中。 – CodesInChaos 2014-12-02 11:49:54

+0

@CodesInChaos:这是一个桌面应用程序。我正在使用VS2013社区版。这篇文章引用了一个清单文件,我没有在我的项目中使用这个清单文件,而且我似乎无法添加它。这篇文章明确提到了那个我没有的清单。 – 2014-12-02 11:57:20

回答

8

问这个问题后,我偶然发现了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。我没有看到我检查过的任何其他注册处理程序都使用了相同的密钥,因此我将其排除在外并且无法检测到任何问题。

另一个关键问题是,如果我的设置处理程序的应用程序是32位,所有这些都不起作用。当在Wow6432Node中创建条目时,我无法选择处理程序作为给定协议的默认值。我花了一段时间才弄明白,因为我的应用程序被编译为AnyCPU。我第一次错过了这个小标志在项目属性:

enter image description here

相关问题