找到本地的exe文件并打开

简介: 找到本地的exe文件并打开

DEMO

<template>
  <div>
    <a v-bind:href="'notepad://C:/Windows/System32/notepad.exe'" @click.prevent="openApp">打开 Notepad</a>
  </div>
</template>
<script>
export default {
  methods: {
    openApp() {
      window.open('notepad://C:/Windows/System32/notepad.exe');
    }
  }
}
</script>
function findLocalExecutable(exeName) {
  const shell = require('child_process').execSync;
  try {
    // 使用 where 命令在系统路径中查找可执行文件
    const result = shell(`where ${exeName}`).toString();
    // 处理多个匹配结果,只返回第一个结果
    const paths = result.split('\r\n');
    if (paths.length > 0) {
      return paths[0];
    }
  } catch (error) {
    // 如果未找到可执行文件,会抛出异常
    console.error(`未找到 ${exeName}`);
  }
  return null;
}

推荐使用第二种方法

1. reg文件 需要点注册表

新建EpicGamesLauncher.reg 之后在双击

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\EpicGamesLauncher]
@="URL:EpicGamesLauncher"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\EpicGamesLauncher\shell]
[HKEY_CLASSES_ROOT\EpicGamesLauncher\shell\open]
[HKEY_CLASSES_ROOT\EpicGamesLauncher\shell\open\command]
@="D:\\Epic Games\\Launcher\\Portal\\Binaries\\Win32\\EpicGamesLauncher.exe"

@="D:\\Epic Games\\Launcher\\Portal\\Binaries\\Win32\\EpicGamesLauncher.exe" EpicGamesLauncher是软件名 建议有该单词都改成自己需要打开的文件名

! 注意路劲双\ 必须是绝对路劲

js文件 需要导出

/**
 * 打开本地可执行的exe程序
 * @param href
 */
export function openExe (href) {
  const a = document.createElement('a')
  a.setAttribute('href', href)
  a.click()
}

html 导入

import { openExe} from

const openpc = ()=>{
    openExe('EpicGamesLauncher://')
}

第二种方法 推荐使用

  1. 新建protocolcheck.js文件内容如下
(function (f) {
    if (typeof exports === "object" && typeof module !== "undefined") {
      module.exports = f();
    } else if (typeof define === "function" && define.amd) {
      define([], f);
    } else {
      var g;
      if (typeof window !== "undefined") {
        g = window;
      } else if (typeof global !== "undefined") {
        g = global;
      } else if (typeof self !== "undefined") {
        g = self;
      } else {
        g = this;
      }
      g.protocolCheck = f();
    }
  })(function () {
    var define, module, exports;
    return (function e(t, n, r) {
      function s(o, u) {
        if (!n[o]) {
          if (!t[o]) {
            var a = typeof require == "function" && require;
            if (!u && a) return a(o, !0);
            if (i) return i(o, !0);
            var f = new Error("Cannot find module '" + o + "'");
            throw ((f.code = "MODULE_NOT_FOUND"), f);
          }
          var l = (n[o] = { exports: {} });
          t[o][0].call(
            l.exports,
            function (e) {
              var n = t[o][1][e];
              return s(n ? n : e);
            },
            l,
            l.exports,
            e,
            t,
            n,
            r
          );
        }
        return n[o].exports;
      }
      var i = typeof require == "function" && require;
      for (var o = 0; o < r.length; o++) s(r[o]);
      return s;
    })(
      {
        1: [
          function (require, module, exports) {
            function _registerEvent(target, eventType, cb) {
              if (target.addEventListener) {
                target.addEventListener(eventType, cb);
                return {
                  remove: function () {
                    target.removeEventListener(eventType, cb);
                  },
                };
              } else {
                target.attachEvent(eventType, cb);
                return {
                  remove: function () {
                    target.detachEvent(eventType, cb);
                  },
                };
              }
            }
            function _createHiddenIframe(target, uri) {
              var iframe = document.createElement("iframe");
              iframe.src = uri;
              iframe.id = "hiddenIframe";
              iframe.style.display = "none";
              target.appendChild(iframe);
              return iframe;
            }
            function openUriWithHiddenFrame(uri, failCb, successCb) {
              var timeout = setTimeout(function () {
                failCb();
                handler.remove();
              }, 1000);
              var iframe = document.querySelector("#hiddenIframe");
              if (!iframe) {
                iframe = _createHiddenIframe(document.body, "about:blank");
              }
              var handler = _registerEvent(window, "blur", onBlur);
              function onBlur() {
                clearTimeout(timeout);
                handler.remove();
                successCb();
              }
              iframe.contentWindow.location.href = uri;
            }
            function openUriWithTimeoutHack(uri, failCb, successCb) {
              var timeout = setTimeout(function () {
                failCb();
                handler.remove();
              }, 1000);
              //handle page running in an iframe (blur must be registered with top level window)
              var target = window;
              while (target != target.parent) {
                target = target.parent;
              }
              var handler = _registerEvent(target, "blur", onBlur);
              function onBlur() {
                clearTimeout(timeout);
                handler.remove();
                successCb();
              }
              window.location = uri;
            }
            function openUriUsingFirefox(uri, failCb, successCb) {
              var iframe = document.querySelector("#hiddenIframe");
              if (!iframe) {
                iframe = _createHiddenIframe(document.body, "about:blank");
              }
              try {
                iframe.contentWindow.location.href = uri;
                successCb();
              } catch (e) {
                if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
                  failCb();
                }
              }
            }
            function openUriUsingIEInOlderWindows(uri, failCb, successCb) {
              if (getInternetExplorerVersion() === 10) {
                openUriUsingIE10InWindows7(uri, failCb, successCb);
              } else if (
                getInternetExplorerVersion() === 9 ||
                getInternetExplorerVersion() === 11
              ) {
                openUriWithHiddenFrame(uri, failCb, successCb);
              } else {
                openUriInNewWindowHack(uri, failCb, successCb);
              }
            }
            function openUriUsingIE10InWindows7(uri, failCb, successCb) {
              var timeout = setTimeout(failCb, 1000);
              window.addEventListener("blur", function () {
                clearTimeout(timeout);
                successCb();
              });
              var iframe = document.querySelector("#hiddenIframe");
              if (!iframe) {
                iframe = _createHiddenIframe(document.body, "about:blank");
              }
              try {
                iframe.contentWindow.location.href = uri;
              } catch (e) {
                failCb();
                clearTimeout(timeout);
              }
            }
            function openUriInNewWindowHack(uri, failCb, successCb) {
              var myWindow = window.open("", "", "width=0,height=0");
              myWindow.document.write("<iframe src='" + uri + "'></iframe>");
              setTimeout(function () {
                try {
                  myWindow.location.href;
                  myWindow.setTimeout("window.close()", 1000);
                  successCb();
                } catch (e) {
                  myWindow.close();
                  failCb();
                }
              }, 1000);
            }
            function openUriWithMsLaunchUri(uri, failCb, successCb) {
              navigator.msLaunchUri(uri, successCb, failCb);
            }
            function checkBrowser() {
              var isOpera =
                !!window.opera || navigator.userAgent.indexOf(" OPR/") >= 0;
              var ua = navigator.userAgent.toLowerCase();
              return {
                isOpera: isOpera,
                isFirefox: typeof InstallTrigger !== "undefined",
                isSafari:
                  (~ua.indexOf("safari") && !~ua.indexOf("chrome")) ||
                  Object.prototype.toString
                    .call(window.HTMLElement)
                    .indexOf("Constructor") > 0,
                isIOS:
                  /iPad|iPhone|iPod/.test(navigator.userAgent) &&
                  !window.MSStream,
                isChrome: !!window.chrome && !isOpera,
                isIE: /*@cc_on!@*/ false || !!document.documentMode, // At least IE6
              };
            }
            function getInternetExplorerVersion() {
              var rv = -1;
              if (navigator.appName === "Microsoft Internet Explorer") {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
                if (re.exec(ua) != null) rv = parseFloat(RegExp.$1);
              } else if (navigator.appName === "Netscape") {
                var ua = navigator.userAgent;
                var re = new RegExp("Trident/.*rv:([0-9]{1,}[.0-9]{0,})");
                if (re.exec(ua) != null) {
                  rv = parseFloat(RegExp.$1);
                }
              }
              return rv;
            }
            module.exports = function (uri, failCb, successCb, unsupportedCb) {
              function failCallback() {
                failCb && failCb();
              }
              function successCallback() {
                successCb && successCb();
              }
              if (navigator.msLaunchUri) {
                //for IE and Edge in Win 8 and Win 10
                openUriWithMsLaunchUri(uri, failCb, successCb);
              } else {
                var browser = checkBrowser();
                if (browser.isFirefox) {
                  openUriUsingFirefox(uri, failCallback, successCallback);
                } else if (browser.isChrome || browser.isIOS) {
                  openUriWithTimeoutHack(uri, failCallback, successCallback);
                } else if (browser.isIE) {
                  openUriUsingIEInOlderWindows(
                    uri,
                    failCallback,
                    successCallback
                  );
                } else if (browser.isSafari) {
                  openUriWithHiddenFrame(uri, failCallback, successCallback);
                } else {
                  unsupportedCb();
                  //not supported, implement please
                }
              }
            };
          },
          {},
        ],
      },
      {},
      [1]
    )(1);
  });
  1. 项目的index.html 载入文件 <script src="./src/utils/protocolcheck.js"></script> //注意自己的路劲 随便位置就行
  2. 需要打开exe页面html:
window.protocolCheck(openUrl, () => {
      alert('下载')
  });

如果报错 window.protocolCheck is not a function

请在需要打开的页面导入该protocolcheck文件 import '@/utils/protocolcheck.js'

相关文章
如何在cmd中打开指定文件夹路径
如何在cmd中打开指定文件夹路径
1049 0
win10下双击运行.bat文件,一闪而过
直接在cmd运行.bat里面的命令可以运行,但是双击.bat就是一闪而过,那么为什么会出现这样的问题呢,一般情况是.bat里面的命令,在双击运行的环境下出现了问题导致的。
1512 0
win10下双击运行.bat文件,一闪而过
|
1月前
|
C# Windows
C#实现指南:将文件夹与exe合并为一个exe
C#实现指南:将文件夹与exe合并为一个exe
133 9
|
1月前
|
存储 机器学习/深度学习 数据可视化
解析exe文件
如何使用`objdump`工具解析exe文件,包括exe文件的组成、`objdump`的用法以及如何查看exe文件的节头信息和完整内容。
66 0
解析exe文件
|
6月前
win11查看文件/文件夹所使用的程序
win11查看文件/文件夹所使用的程序
53 0
|
Shell Windows
Windows在当前路径下打开cmd不用复制粘贴
Windows在当前路径下打开cmd不用复制粘贴
122 3
Win10 需要提供管理员权限才能复制到此文件夹的解决方法
Win10 需要提供管理员权限才能复制到此文件夹的解决方法
227 0
Win10 需要提供管理员权限才能复制到此文件夹的解决方法
|
NoSQL C语言 C++
Vscode修改.exe文件生成位置
Vscode修改.exe文件生成位置
868 0
Vscode修改.exe文件生成位置
|
Windows
Win系统 - 文件夹或文件已在另一程序中打开怎么办?
Win系统 - 文件夹或文件已在另一程序中打开怎么办?
581 0
Win系统 - 文件夹或文件已在另一程序中打开怎么办?