從網頁中喚起桌面應用程序

在此,簡單地記錄一下如何實現從瀏覽器網頁中啓動桌面應用程序,以備查閱。

我們可以分三步實現:

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>

這是測試效果:

發佈了20 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章