1、项目需求:在a程序中点击打开b程序
2、代码实现:
index.html:
//按钮点击事件
machineBtn.click(()=>{
let exePath;
//exePath.config文件里面是b程序的路径
fs.readFile('exePath.config', 'utf-8', function(err, data) {
exePath = data;
ipcRenderer.send('open-child',exePath);
})
})
main.js:
let spawn = [];
ipcMain.on('open-child',(e,msg)=>{
var childSpawn = childProcess.spawn(msg);
spawn[spawn.length] = childSpawn;
})
补充:关闭程序
index.html:
ipcRenderer.send('close-child',exePath);
main.js:
ipcMain.on('close-child',(e,msg)=>{
console.log("close",msg)
for(var i = 0;i<spawn.length;i++){
spawn[i].kill();
}
spawn = [];
})