公司打印插件有個(gè)需求,需要瀏覽器點(diǎn)擊去啟動(dòng)那個(gè)插件程序,網(wǎng)上搜了下,解決的思路是通過URL Protocol去調(diào)起桌面程序
1.需要在c#程序中添加注冊(cè)表,或者手動(dòng)添加注冊(cè)表
static void RegisterMyProtocol(string myAppPath) //myAppPath = full path to your application
{
RegistryKey key = Registry.ClassesRoot.OpenSubKey("myApp"); //open myApp protocol's subkey
if (key == null) //if the protocol is not registered yet...we register it
{
key = Registry.ClassesRoot.CreateSubKey("myApp");
key.SetValue(string.Empty, "URL: myApp Protocol");
key.SetValue("URL Protocol", string.Empty);
key = key.CreateSubKey(@"shell\open\command");
key.SetValue(string.Empty, myAppPath + " " + "%1");
//%1 represents the argument - this tells windows to open this program with an argument / parameter
}
key.Close();
}
2.在瀏覽器頁面中打開
window.open("myApp://")
參考:
https://codingvision.net/c-register-a-url-protocol
https://medium.com/front-end-weekly/launching-desktop-application-from-browser-using-custom-protocol-c-598c4519c839