创建一个按钮来执行.bat或.exe

时间:2018-04-16 13:09:23

标签: javascript html node.js electron

我是Electron和JS的新手。

我搜索了一个解决方案来创建一个执行.bat文件或.exe文件的简单按钮。

关于使用child_process,我已阅读此article

但是,它没有说明如何将var“链接”到我的按钮。

我的代码是用renderer.js

编写的

2 个答案:

答案 0 :(得分:2)

电子使用nodejs运行,因此您可以执行以下操作:

var execFile = require('child_process').execFile;

    var runExe = function(){
       execFile('<your-name>.exe', function(err, data) {  
            console.log(err)
            console.log(data.toString());                       
        });  
    }

现在打电话 runExe() 使用你的按钮,你应该很高兴

了解更多信息,请点击此处 node js reference

所以发生的事情基本上是我们运行一个指定的exe文件,就像你已经说过nodejs child_process ...希望有所帮助

答案 1 :(得分:1)

好的,现在是我发布电子解决方案的工作:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <!-- All of the Node.js APIs are available in this renderer process. -->
    We are using Node.js <script>document.write(process.versions.node)</script>,
    Chromium <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
	  <h1> A simple Javascript created button </h1>

     <button onclick="function()">Firefox 1</button>
     <button onclick="firefox()">Firefox 2</button>

    <script>
var execFile = require('child_process').execFile;

function firefox(){
       execFile("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", function(err, data) {  
            console.log(err)
            console.log(data.toString());                       
        });  
    }

		// You can also require other files to run in this process
      require('./renderer.js')
	</script>
  </body>
</html>

相关问题