Giter Site home page Giter Site logo

demo-register-app-custom-url-protocol's Introduction

使用 URL 開啟 Windows 應用程式

註冊自定的 URL Protocol

要註冊一組 URL Protocol 並設定呼叫該 URL 時啟動指定的程式,所需要的登錄設定如下:

HKEY_CLASSES_ROOT
   YourAppProtocol
      (Default) = "URL:Your App Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "YourAppName.exe,1"
      shell
         open
            command
               (Default) = "C:\YourAppNamePath\YourAppName.exe" "parameter=%1"

寫成註冊檔了話,可以參考下面的寫法:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\YourAppProtocol]
@="URL:Your App Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\YourAppProtocol\DefaultIcon]
@="\"YourAppName.exe\",1"

[HKEY_CLASSES_ROOT\YourAppProtocol\shell]

[HKEY_CLASSES_ROOT\YourAppProtocol\shell\open]

[HKEY_CLASSES_ROOT\YourAppProtocol\shell\open\command]
@="\"C:\\YourAppNamePath\\YourAppName.exe\" \"parameter=%1\""

這程式碼有些地方你需要修改成你的設定,例如 YourAppProtocol 這是要使用的協定名稱,註冊後就可以使用 YourAppProtocol: 此協定來呼叫你的應用程式,另一個重點是 shell\open\command 這個設定下的參數,這是你應用程式的所在位置,如果你需要接收參數,可以在後面加上一些參數資訊,例如以上面的程式碼來說,我使用 parameter=%1 來接收 %1 這個傳入的資訊。

你可以複製上面的程式碼,修改成你所需要的設定,另存成副檔名為 reg 的檔案並執行,即可完成註冊,註冊後可以使用 regedit.msc 這隻 Windows 程式查看登錄檔內容,就可以看到該設定,如下圖:

註冊後可以在登錄檔中看到該設定

測試程式

光看登錄檔的設定其實沒甚麼感覺,我們來寫一個測試用的 YourAppName.exe 來試試看。

這隻程式動作簡單,就只是把所接收到的參數資訊轉成 JSON 格式,然後印在終端機上,程式碼如下:

static void Main(string[] args)
{
    Console.WriteLine("Launch App!");

    Console.WriteLine("Received Arguments: ");
    var arguments = args
        .Select(argument => argument.Split('='))
        .Where(split => split.Length == 2)
        .ToDictionary(split => split[0], split => split[1]);
    Console.WriteLine(JsonSerializer.Serialize(arguments));
    Console.ReadKey();
}

發行程 EXE 可執行檔後,複製到登錄檔所設定的位置 C:\YourAppNamePath\YourAppName.exe,接者在瀏覽器的網址列輸入 YourAppProtocol:YourParameter,瀏覽器會詢問你是否要啟動 YourAppName 應用程式:

瀏覽器詢問是否開啟 YourAppName 應用程式

點選 Open 開啟應用程式後,可以看到以下的輸出畫面:

啟動應用程式後將所接收到參數後印出來

我們可以清楚的看到所接收到的參數長甚麼樣子,如此一來就可以再去思考要如何和本機所安裝的應用程式如何互動了。

移除登錄檔

整個測試完成後,可以使用下面的程式來移除登錄檔:

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\YourAppProtocol]

你可以複製上面的程式碼,修改 YourAppProtocol 成你所需要的設定,另存成副檔名為 reg 的檔案並執行,即可完成移除登錄檔

後記

這篇測試範例所用到的程式都放在 poychang/demo-register-app-custom-url-protocol 這裡,有相關需求的開發者可以直接取用。

  • YourAppName.sln 主控台應用程式
  • register-custom-url-protocol.reg 註冊登錄檔
  • delete-custom-url-protocol.reg 移除登錄檔

demo-register-app-custom-url-protocol's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.