在 Windows 8 上的 C# 中注册自定义 URL 处理程序 [英] Registering Custom URL Handler in C# on Windows 8

查看:29
本文介绍了在 Windows 8 上的 C# 中注册自定义 URL 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
        }
    }
}

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

the above class was pieced together from snippets of code I found on the net, specifically:

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

http://dotnet-snippets.com/snippet/register-user-defined-url-protocol/2707 <-- works for Win7 but not Win8

这让我找到了在 Windows 8 中注册协议处理程序 这是一个未经证实的答案.

which led me to find Registering a protocol handler in Windows 8 which is an unconfirmed answer.

然而,我无法让 URL 协议在 Win8 中工作;单击 aodb://1234 超链接不会启动应用程序,并且网络浏览器抱怨不支持该协议,我认为上述文章不是正确答案.

HOWEVER, I can't get the URL protocol to work in Win8; clicking an aodb://1234 hyperlink does not launch the application and the web browser complains that the protocol isn't supported, and I believe the above article is not a proper answer.

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

does anyone who has knowledge of protocol handlers, know where I went wrong in the above code and why the protocol doesn't register in win8? I can see the above code worked by looking at the registry keys in regedit, but for some reason, the protocol isn't recognized.

推荐答案

我明白了!最后.好吧,看来Win8及以上的Win7和Win8的注册表功能都要实现,不过Win7不需要额外的代码.下面是注册自定义协议的最终类.

I GOT IT! Finally. Ok, it seems that you have to implement both Win7 and Win8 registry functions for Win8 and above, but Win7 doesn't require the extra code. below is the final class for registering a custom 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);
        }
    }
}

这篇关于在 Windows 8 上的 C# 中注册自定义 URL 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆