【VUE】Windows下带参启动本地应用程序

一、Windows URL 协议

URL Protocol服务的协议,简单说是点击一个网页的链接,通过这个链接执行计算机上的一个指定程序,并向其传递相应的信息数据。通过一个注册表文件可以了解其工作原理。
 
WinURLProtocol.reg
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\WinURLProtocol]
@="WinURLProtocol protocol"
"URL Protocol"="D:\\Program Files (x86)\\ToolBox\\App\\SmartPlayer\\WinURLProtocol.exe"

[HKEY_CLASSES_ROOT\WinURLProtocol\DefaultIcon]
@="\"D:\\Program Files (x86)\\ToolBox\\App\\SmartPlayer\\WinURLProtocol.exe\",0"

[HKEY_CLASSES_ROOT\WinURLProtocol\shell]
@=""

[HKEY_CLASSES_ROOT\WinURLProtocol\shell\open]
@=""

[HKEY_CLASSES_ROOT\WinURLProtocol\shell\open\command]
@="\"D:\\Program Files (x86)\\ToolBox\\App\\SmartPlayer\\WinURLProtocol.exe\" \"%1\""

在注册表首先注册了URL Protocol服务,然后将这个服务与一个程序关联起来。这里的程序就是WinURLProtocol.exe。当点击一个网页链接的时候,这个链接就会启动执行WinURLProtocol.exe程序,并向其传送一个URL编码字符串信息。

二、URL Protocol服务的使用

2.1、无参数启动

为WinURLProtocol程序注册URL服务以后, 网页连接的格式如下所示:
<a href="WinURLProtocol://">打开WinURLProtocol程序</a>

2.1、带参启动

例 、VUE 视频播放共享文件夹的视频文件

VUE template部分格式

<a style="color:blue;text-decoration:underline" :href="helpDetails.url_vlog_full" target="_blank">发药结果 - 短视频</a>

VUE script部分格式 

helpDetails.url_vlog_full=
WinURLProtocol://"D:\Program Files (x86)\ToolBox\App\SmartPlayer\SmartPlayer.exe" "\\172.16.75.165\share\202009\12345.dav"

 WinURLProtocol 接收参数为

大部分第三方软件并不能解析url编码的参数,根据url的编码特性,建议使用熟悉的编程语言编写一个简单串联程序,如:四、使用C#编一个简单的串联程序,第一个参数为第三方软件,第二个参数为第三方软件的参数

winurlprotocol://%22D:%5CProgram%20Files%20(x86)%5CToolBox%5CApp%5CSmartPlayer%5CSmartPlayer.exe%22%20%22D:%5Cdata.dav%22/

三、URL编码 

url编码解码,又叫百分号编码,是统一资源定位(URL)编码方式。URL地址(常说网址)规定了常用地数字,字母可以直接使用,另外一批作为特殊用户字符也可以直接用(/,:@等),剩下的其它所有字符必须通过%xx编码处理。 现在已经成为一种规范了,基本所有程序语言都有这种编码,如js:有encodeURI、encodeURIComponent,PHP有 urlencode、urldecode等。编码方法很简单,在该字节ascii码的的16进制字符前面加%. 如 空格字符,ascii码是32,对应16进制是'20',那么urlencode编码结果是:%20

如果您使用的程序/第三方不支持 Windows URL 协议,您则需要另外开发一个简单的软件,将页面操作与第三方软件串联起来,如使用C#写一个简单的串联程序

四、C#串联程序 WinURLProtocol

此程序的功能,是接收页面点击传入的url参数,并将URL解码成System.Diagnostics.Process能识别的启动路径 + 参数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinURLProtocol
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                string start = "://";
                int index = args[0].IndexOf(start);
                string urlParams = args[0].Substring(index+start.Length,args[0].Length - index-start.Length-1);
                // URL 解码
                string utf8Params = System.Web.HttpUtility.UrlDecode(urlParams, System.Text.Encoding.UTF8);
                string[] exe = utf8Params.Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
                if(exe.Length == 1)
                {
                    System.Diagnostics.Process.Start(@"""" + exe[0] + @"""");
                }
                else if (exe.Length == 2)
                {
                    System.Diagnostics.Process.Start(@"""" + exe[0] + @"""", @"""" + exe[1] + @"""");
                }
                else if (exe.Length == 3)
                {
                    System.Diagnostics.Process.Start(@"""" + exe[0] +@"""", @"""" + exe[2] + @"""");
                }
                else
                {

                    System.Diagnostics.Process.Start(utf8Params);
                }
            }
            else
            {
                string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                string name = System.IO.Path.GetFileNameWithoutExtension(path);
                string regFile = System.AppDomain.CurrentDomain.BaseDirectory + name + ".reg";
                Write(path,name, regFile);
                if (System.IO.File.Exists(regFile))
                {
                    System.Diagnostics.Process.Start("regedit", @"""" + regFile + @"""");
                }
            }
        }
        /// <summary>
        /// URL Protocol 服务协议注册程序
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <param name="regFile"></param>
        public static void Write(string path,string name,string regFile)
        {
            try
            {
                string zyPath = path.Replace(@"\", @"\\");

                if(System.IO.File.Exists(regFile))
                {
                    System.IO.File.Delete(regFile);
                }
                List<string> contents = new List<string>();
                contents.Add("Windows Registry Editor Version 5.00");
                contents.Add("");
                contents.Add(string.Format(@"[HKEY_CLASSES_ROOT\{0}]", name));
                contents.Add(string.Format(@"@=""{0} protocol""", name));
                contents.Add(string.Format(@"""URL Protocol""=""{0}""", zyPath));
                contents.Add("");
                contents.Add(string.Format(@"[HKEY_CLASSES_ROOT\{0}\DefaultIcon]", name));
                contents.Add(string.Format(@"@=""\""{0}\"",0""", zyPath));
                contents.Add("");
                contents.Add(string.Format(@"[HKEY_CLASSES_ROOT\{0}\shell]", name));
                contents.Add(string.Format(@"@=""""", name));
                contents.Add("");
                contents.Add(string.Format(@"[HKEY_CLASSES_ROOT\{0}\shell\open]", name));
                contents.Add(string.Format(@"@=""""", name));
                contents.Add("");
                contents.Add(string.Format(@"[HKEY_CLASSES_ROOT\{0}\shell\open\command]", name));
                contents.Add(string.Format(@"@=""\""{0}\"" \""%1\""""", zyPath));

                System.IO.File.WriteAllLines(regFile,contents.ToArray());
            }
            catch (Exception exp)
            {
                if (exp!=null)
                {
                    System.Windows.Forms.MessageBox.Show(exp.Message);
                    if (exp.InnerException != null)
                        System.Windows.Forms.MessageBox.Show(exp.InnerException.Message);
                }

            }
        }
    }
}

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值