html中调用本地exe程序 打开文件或文件夹

2016-5-1 12:22:50  补充

      关于a标签中的href被浏览器编码。昨晚说的空格和& " 情况是在火狐(42.0)中,chrome中的情况是:中文也会被urlencode。

      我的href中格式是  Mytest:filepath=xxxx;exepath=xxxx;type=open。 c#中先根据分号分组,再根据等号写入json。最关键的点在于xxx能正确被c#程序解码。

     微软官网说 System.Net 的 WebUtility.UrlDecode方法 将已经为在 URL 中传输而编码的字符串转换为解码的字符串。但是经过测试 发现有一大缺点,+号被转为空格了! 我直接说吧,百度了不少文章,发现一篇说 用Uri.UnescapeDataString在System 命名空间中,不需要额外的Dll引用)。整篇博文就这一句,顶过别人的1000字。


和刚发的右键新建html文章一样,这个都是以前搞的。最近为自己做个单页应用,跟浏览器书签一样的,网址+描述,保存到数据库。然后想将一些自己常用的其他文件和文件夹放过来,能快速打开。

这就是我的需求。起因:前端们的浏览器都是有多个的,添加书签不方便,重装浏览器或系统都麻烦;常用的文件和文件夹,经常要win+e --> 进入文件夹 --> 进入文件夹 ... 巨麻烦。

风险评估。这个小项目我有能力完成吗,要多长时间?

html中调用本地exe程序以前就完成过简单功能。起因是发现网页中需要qq客服这玩意,发现能调本地程序,很好奇就百度了。对于我来说,确实很有用完成了不少需求。

第一步,在注册表中新建自己的协议。

在图中的command 默认字符串 改为  "D:\wince\获取传递参数\获取传递参数\bin\Debug\获取传递参数.exe" "%1" 。

要调用的程序 后面的%1是调用程序时传入的参数。如播放音乐传入音乐文件的路径。

第二步,在网页的a标签中 href="MyTest:xxx" ,xxx代表参数。冒号后加不加斜杠都可以。但传给程序的是这个href的值,而且需要注意的是,空格会被转为%20,&会被过滤,双引号会被转为%22!!!

我的目的是打开文件或文件夹。如果是文件,还要看是否指定了打开文件的程序。程序可以隐藏运行,查杀进程只有一个实例。

我写的c#代码:真实可用 就不贴图了

//这个要引入dll   要求本程序的框架集为4.0版本
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
//判断目录是否存在
using System.IO;
//把空格%20还原
using System.Text.RegularExpressions;
 
public Form1(string args)//添加的构造函数 
        {
            InitializeComponent();
            //1.去掉协议 mytest:
            args = args.Substring(args.IndexOf(":")+1);
            //2.将字符数组转为json对象
            string[] arr = args.Split(';');
            JObject o = (JObject)JsonConvert.DeserializeObject("{}");
            for (int i = 0; i < arr.Length; i++)
            {
                string []temp = arr[i].Split('=');
                if (temp[0] != "")
                {
                    //将被转义的空格还原
                    o[temp[0]] = unescape(temp[1]);
                    //将Unicode转中文
                    
                }
            }
            label1.Text = o.ToString();
            //3.键值存在 值不为空
            if (!(o.Property("type") == null || o.Property("type").ToString() == ""))
            {
                // 打开命令 指定文件存在
                if (o["type"].ToString() == "open" && o.Property("filepath")!=null && File.Exists(o["filepath"].ToString()))
                {
                    //指定的打开方式存在
                    if(o.Property("exepath")!=null&&File.Exists(o["exepath"].ToString())){
                        Process process = new Process();
                        ProcessStartInfo proStartInfo = new ProcessStartInfo(o["exepath"].ToString(), o["filepath"].ToString());
                        process.StartInfo = proStartInfo;
                        process.Start();
                    }
                    //默认程序打开
                    else
                    {
                        Process process = new Process();
                        process.StartInfo.FileName = o["filepath"].ToString();
                        process.StartInfo.Verb = "Open";
                        process.StartInfo.CreateNoWindow = true;
                        process.Start();
                    }
                }
                //打开文件夹
                if (o["type"].ToString() == "open" && o.Property("filepath") != null && Directory.Exists(o["filepath"].ToString()))
                {
                    System.Diagnostics.Process.Start("explorer.exe", o["filepath"].ToString());
                }
            }
            /*
            //杀死相同实例的进程 只允许存在一个实例
            KillProcess(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
            //完成使命后自杀
            System.Environment.Exit(0);
            */
        }
        //程序隐藏运行  可以根据需要修改代码
        private void Form1_Load(object sender, EventArgs e)
        {
            //this.Hide();
            //this.ShowInTaskbar = false;
        }
        //查杀进程
        private void KillProcess(string processName)
        {
            Process[] myproc = Process.GetProcesses();
            int processid = System.Diagnostics.Process.GetCurrentProcess().Id;
            foreach (Process item in myproc)
            {
                if (item.ProcessName == processName&&item.Id!=processid)
                {
                    item.Kill();
                }
            }
        }
        //用于将空格%20还原
        public static string unescape(string str)
        {
            StringBuilder sb = new StringBuilder();
            int len = str.Length;
            int i = 0;
            while (i != len)
            {
                if (Uri.IsHexEncoding(str, i))
                    sb.Append(Uri.HexUnescape(str, ref i));
                else
                    sb.Append(str[i++]);
            }
            return sb.ToString();
        }


你可能感兴趣的:(WEB)