问题描述
我正在尝试使按钮在计算机中打开exe文件,但无法打开,并且出现此错误
I am trying to make a button open an exe file in computer but it doesn't open and it gives me this error
这是我的按钮的代码
<button onclick="runExe()" id="button">click</button>
我从互联网上获得了此代码,当我删除函数runExe()时,exe文件打开当我启动该应用程序并且希望它仅在单击按钮时打开。
and i got this code form the internet and when i remove the function runExe() the exe file opens when i start the app and i want it to only open when button is clicked.
这是打开exe文件的代码
here is the code to open the exe file
function runExe(){
var child = require('child_process').execFile;
var executablePath = "winRAR.exe";
child(executablePath, function(err, data) {
if(err){
console.error(err);
return;
}
console.log(data.toString());
});}
推荐答案
错误消息提示您不能使用 require
。
The error message says you cannot use require
.
您正在尝试在渲染器进程中运行该代码。如果main和renderer之间的区别是新的,请参见
You are trying to run that code in the renderer process. If the distinction between main and renderer is new, see https://www.electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
一种做自己想要的方法相反,是让呈现的流程将打开的请求发送到主流程,在该流程中,需求将在其中运行,在哪个子流程应该在其中打开。
One way to do what you want is to instead have the rendered process send the open request to the main process, where the require will work, and where opening a child process should work.
BTW,我不知道不知道 shell.openItem()
()可用于启动任何exe吗?如果是这样,那可能是更好的方法。
BTW, I don't know if shell.openItem()
(https://www.electronjs.org/docs/api/shell) can be used to start any exe? If so, that might be the better way to do it.
这篇关于如何使用电子中的按钮打开exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!