纸上得来终觉浅,绝知此事要躬行。

 

通过点击网页链接打开应用程序——自定义协议

使用场景

如果开发的项目有客户端还有网站,那就可能考虑增加此功能,它可以让用户获取更好的体验。已使用类似功能的产品:腾讯QQ、阿里旺旺等,这里只列出了大家平时最常用的。下面介绍开发流程。

注册自定义协议

/// <summary>
/// 注册自定义协议。
/// </summary>
public class RegisterCustomProtocolTest
{
    static void Main(string[] args)
    {
        Console.WriteLine("Confirm register Custom Protocol ?");
        Console.ReadKey(true);

        var aoo = Registry.ClassesRoot.CreateSubKey("aoo", RegistryKeyPermissionCheck.ReadWriteSubTree);

        //添加Aoo键
        aoo.SetValue("", "URL: Aoo Application 协议");
        aoo.SetValue("URL Protocol", "");

        //添加DefaultIcon
        var iconKey = aoo.CreateSubKey("DefaultIcon");

        iconKey.SetValue("", String.Format("{0},1", @"E:\Test\CustomUrlProtocol\CustomUrlProtocol\bin\Release\CustomUrlProtocol.exe"));

        var shellKey = aoo.CreateSubKey("shell");
        var openKey = shellKey.CreateSubKey("open");
        var commandKey = openKey.CreateSubKey("command");

        commandKey.SetValue("", String.Format("\"{0}\" \"%1\"", @"E:\Test\CustomUrlProtocol\CustomUrlProtocol\bin\Release\CustomUrlProtocol.exe"));
        aoo.Close();

        Console.WriteLine("Register Custom Protocol success!");
        Console.WriteLine("Press anykey to exit ...");
        Console.ReadKey(true);
    }
}

注册成功后,可在 KEY_CLASSES_ROOT 的子键中看到如下子键:

测试自定义协议

假设我的应用程序在 E:\Test\CustomUrlProtocol\CustomUrlProtocol\bin\Release\CustomUrlProtocol.exe 这个位置,上面注册代码中可以看到。新建一个空白网页,添加如下 a 标签:

<a href="aoo:JRoger">TEST_CUSTOM_PROTOCOL</a>

 使用谷歌或IE或FireFox打开这个网页点击此链接,将会弹出一个类似QQ或阿里旺旺启动本地应用程序的窗口,点确认启动就打开了本地应用。

posted on 2013-09-02 17:58  JRoger  阅读(1164)  评论(0编辑  收藏  举报

导航