最近有個客戶提出想從網站上啟動一個客戶端的程序,研究了下,實現方法如下:
1. 注入注冊表
try
{
string appPath = "\"" + Application.ExecutablePath + "\" \"%1\"";
string strKey = Application.ProductName;
string strKey_shell_open_cmd = strKey + @"\shell\open\command";
var subKey = Registry.ClassesRoot.CreateSubKey(strKey);
subKey.SetValue("URL Protocol", "1");
var subKey_shell_open_cmd = Registry.ClassesRoot.CreateSubKey(strKey_shell_open_cmd);
subKey_shell_open_cmd.SetValue(null, appPath);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Web啟動參數設置失敗, 原因: {0}", ex.Message));
}
注入后效果如下圖:
%1是傳入的參數
2. 網站上加一個link
<a href="HelloWorld://123">Hello World</a>
這里123是傳入的參數
當我們點擊這個link時,就會自動打開這個客戶端軟件。