浏览器启动windows本地程序

2024-08-21  本文已影响0人  VervertomJC

公司打印插件有个需求,需要浏览器点击去启动那个插件程序,网上搜了下,解决的思路是通过URL Protocol去调起桌面程序
1.需要在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

上一篇 下一篇

猜你喜欢

热点阅读