从网页中唤起桌面应用程序

在此,简单地记录一下如何实现从浏览器网页中启动桌面应用程序,以备查阅。

我们可以分三步实现:

1. 编写一个简单的桌面应用程序,我们可以把它命名为AgentApp。

2. 把AgentApp的唤起协议注册到注册表中,这样它就能被其他应用程序唤起。

3. 开发一个简单的网页,用来唤起AgentApp。

AgentApp

AgentApp是一个简单的桌面应用程序,它能接收两个整数,把两个整数相加并输出结果。它是个简单的桌面程序而已,我们几乎可以用任何自己喜欢的编程语言开发出来。这是用C#写的示例:

using System;

namespace AgentApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nI am an agent application.");

            int sum = 0;
            Console.WriteLine("\nHere are the arguments that I recieved:\n");
            foreach (var arg in args)
            {
                Uri uri = new Uri(arg);

                var decodeArgument = System.Web.HttpUtility.UrlDecode(arg);
                var splitArgs = decodeArgument.Split();

                foreach (var splitArg in splitArgs)
                {
                    int number = 0;
                    if (int.TryParse(splitArg, out number))
                    {
                        sum += number;
                        Console.WriteLine("    {0}", splitArg);
                    }
                }
                    
            }

            Console.WriteLine("\nHere is the result that I calculated from the arguments:\n\n    {0}", sum);
            Console.ReadLine();
        }
    }
}

为AgentApp注册唤起协议

运行以下注册表脚本(RegisterAgentApp.reg)即可:

Windows Registry Editor Version 5.00  
[HKEY_CLASSES_ROOT\AgentApp]  
"URL Protocol"=""
@="AgentApp"
[HKEY_CLASSES_ROOT\AgentApp\DefaultIcon]
@="C:\\path\\to\\your\\AgentApp.exe,1"
[HKEY_CLASSES_ROOT\AgentApp\shell]
[HKEY_CLASSES_ROOT\AgentApp\shell\open]
[HKEY_CLASSES_ROOT\AgentApp\shell\open\command]
@="\"C:\\path\\to\\your\\AgentApp.exe\" \"%1\""

网页端测试程序

我们可以用一个简单超链接去唤起我们的AgentApp。这是一个简单的示例:

<html>
<head>
<title>
    Agent App Tester
</title>
</head>
<body bgcolor="white">  
<h1 align="center">
  <a href="AgentApp:\\www.company.com 2 3">
    <font>Launch agent app with 2 and 3</font>
  </a>
</h1>
</body>
</html>

这是测试效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值