[转]自定义URL Protocol Handler,从网页调用EXE程序

转自:http://www.cnblogs.com/zjneter/archive/2008/01/08/1030066.html

迅雷,电驴等软件可以在浏览器中点击一个url后自动启动,并执行操作。这是咋实现的呢?

要实现这个功能笼共分3步。(我们注册一个xishui:// 这样的 protocol-handler,实现在网页中点击xishui://hello,就弹出一个对话框,上面显示“hello”)
1 按照如下结构建立注册表 
 
其中 [xishui] 是建立在注册表的 [HKEY_CLASSES_ROOT] 主键下。
2 给相关的键赋值






大家注意到上面 command 项的值为 c: est.exe "%1" ,这个"%1"是传递给test.exe的参数。如果我们点击xishui://hello这样的链接 那么%1的值就是“xishui://hello” 这个字符串。

到此我们改写程序生成test.exe了,我们的目标是弹出一个对话框,显示xishui://hello链接中的hello字样。 也就是说我们要用正则表达式来取出"xishui://hello" 中 “xishui://” 后面的部分

我们来写一个控制台程序 

using System;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace test
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string key = Regex.Match(args[0], @"(?<=://).+?(?=:|/|)").Value;
            MessageBox.Show(key);
        }

    }

}


让我把编译生成的test.exe 复制到c:下 
然后 我写了个test.html

<href="xishui://hello">xishui://hello</a>


然后我在浏览器中点这个链接 ,啥效果?你猜



真的调用了的test.exe,并且显示了hello !

原文地址:https://www.cnblogs.com/psim/p/3758623.html