c# - 在 Windows 8 上的 C# 中注册自定义 URL 处理程序

标签 c# windows-8

using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Aodb
{
    internal static class AodbProtocol
    {
        private const string _Protocol = "aodb";
        private const string _ProtocolHandler = "url.aodb";

        private static readonly string _launch = string.Format(
            "{0}{1}{0} {0}%1{0}", (char)34, Application.ExecutablePath);

        private static readonly Version _win8Version = new Version(6, 2, 9200, 0);

        private static readonly bool _isWin8 =
            Environment.OSVersion.Platform == PlatformID.Win32NT &&
            Environment.OSVersion.Version >= _win8Version;

        internal static void Register()
        {
            if (_isWin8) RegisterWin8();
            else RegisterWin7();
        }

        private static void RegisterWin7()
        {
            var regKey = Registry.ClassesRoot.CreateSubKey(_Protocol);
            regKey.SetValue(null, "URL:aodb Protocol");
            regKey.SetValue("URL Protocol", "");

            regKey = regKey.CreateSubKey(@"shell\open\command");
            regKey.SetValue(null, _launch);
        }

        private static void RegisterWin8()
        {
            var regKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes")
                .CreateSubKey(_ProtocolHandler);

            regKey.SetValue(null, _Protocol);

            regKey.CreateSubKey("DefaultIcon")
                .SetValue(null, string.Format(
                    "{0}{1},1{0}", "\"", Application.ExecutablePath));

            regKey.CreateSubKey(@"shell\open\command").SetValue(null, _launch);

            Registry.LocalMachine.CreateSubKey(string.Format(
                @"SOFTWARE\{0}\{1}\Capabilities\ApplicationDescription\URLAssociations",
                Application.CompanyName, Application.ProductName))
                .SetValue(_Protocol, _ProtocolHandler);

            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications")
                .SetValue(Application.ProductName, string.Format(
                    @"SOFTWARE\{0}\Capabilities", Application.ProductName));
        }

        internal static void Unregister()
        {
            if (!_isWin8)
            {
                Registry.ClassesRoot.DeleteSubKeyTree("aodb", false);
                return;
            }

            // extra work required.
            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes")
                .DeleteSubKeyTree(_ProtocolHandler, false);

            Registry.LocalMachine.DeleteSubKeyTree(string.Format(@"SOFTWARE\{0}\{1}",
                Application.CompanyName, Application.ProductName));

            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications")
                .DeleteValue(Application.ProductName);
        }
    }
}

上面的类是我在网上找到的代码片段拼凑而成的,具体来说:

http://dotnet-snippets.com/snippet/register-user-defined-url-protocol/2707 <-- 适用于 Win7 但不适用于 Win8

这让我找到了 Registering a protocol handler in Windows 8这是一个未经证实的答案。

但是,我无法让 URL 协议(protocol)在 Win8 中工作;单击 aodb://1234 超链接不会启动应用程序,网络浏览器会提示该协议(protocol)不受支持,我认为上述文章不是正确的答案。

任何了解协议(protocol)处理程序的人,是否知道我在上面的代码中哪里出错了,为什么协议(protocol)没有在 win8 中注册?通过查看 regedit 中的注册表项,我可以看到上面的代码有效,但由于某种原因,无法识别该协议(protocol)。

最佳答案

我明白了! 最后。好吧,看来Win8及以上版本必须同时实现Win7和Win8的注册表功能,不过Win7不需要额外的代码。 下面是注册自定义协议(protocol)的最后一个类。

using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Aodb
{
    internal static class AodbProtocol
    {
        private const string _Protocol = "aodb";
        private const string _ProtocolHandler = "url.aodb";

        private static readonly string _launch = string.Format(
            "{0}{1}{0} {0}%1{0}", (char)34, Application.ExecutablePath);

        private static readonly Version _win8Version = new Version(6, 2, 9200, 0);

        private static readonly bool _isWin8 =
            Environment.OSVersion.Platform == PlatformID.Win32NT &&
            Environment.OSVersion.Version >= _win8Version;

        internal static void Register()
        {
            if (_isWin8) RegisterWin8();
            else RegisterWin7();
        }

        private static void RegisterWin7()
        {
            var regKey = Registry.ClassesRoot.CreateSubKey(_Protocol);

            regKey.CreateSubKey("DefaultIcon")
                .SetValue(null, string.Format("{0}{1},1{0}", (char)34, 
                    Application.ExecutablePath));

            regKey.SetValue(null, "URL:aodb Protocol");
            regKey.SetValue("URL Protocol", "");

            regKey = regKey.CreateSubKey(@"shell\open\command");
            regKey.SetValue(null, _launch);
        }

        private static void RegisterWin8()
        {
            RegisterWin7();

            var regKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes")
                .CreateSubKey(_ProtocolHandler);

            regKey.SetValue(null, _Protocol);

            regKey.CreateSubKey("DefaultIcon")
                 .SetValue(null, string.Format("{0}{1},1{0}", (char)34,
                     Application.ExecutablePath));

            regKey.CreateSubKey(@"shell\open\command").SetValue(null, _launch);

            Registry.LocalMachine.CreateSubKey(string.Format(
                @"SOFTWARE\{0}\{1}\Capabilities\ApplicationDescription\URLAssociations",
                Application.CompanyName, Application.ProductName))
                .SetValue(_Protocol, _ProtocolHandler);

            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications")
                .SetValue(Application.ProductName, string.Format(
                    @"SOFTWARE\{0}\Capabilities", Application.ProductName));
        }

        internal static void Unregister()
        {
            if (!_isWin8)
            {
                Registry.ClassesRoot.DeleteSubKeyTree("aodb", false);
                return;
            }

            // extra work required.
            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes")
                .DeleteSubKeyTree(_ProtocolHandler, false);

            Registry.LocalMachine.DeleteSubKeyTree(string.Format(@"SOFTWARE\{0}\{1}",
                Application.CompanyName, Application.ProductName));

            Registry.LocalMachine.CreateSubKey(@"SOFTWARE\RegisteredApplications")
                .DeleteValue(Application.ProductName);
        }
    }
}

关于c# - 在 Windows 8 上的 C# 中注册自定义 URL 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626050/

相关文章:

windows-8 - 0x800a1391 - JavaScript 运行时错误 : 'WinJS' is undefined

javascript - 如何在 Windows 8 对话框中包含复选框?

c# - Gmt php 或 UTC C# 通过 SOAP 等效

c# 我是否正确处理多个线程

c# - 如何创建 ObjectSet 的实例?

c# - DbProviderFactory 代码与用于在 ADO.Net 中创建 SqlConnection 的代码的关系

visual-studio-2010 - 默认情况下在 Windows Vista 上下文中运行的应用程序

c# - 禁用应用程序在 Windows 8 Metro 中休眠

C# : return dbnull. 值或函数的 system.type?

c# - Wix 安装项目的 exe "app can' t 在您的 PC 上运行”