我看过一些相关的问题,有一些打开特定文件并在资源管理器窗口中选择它的方法。命令行大致如下:
explorer.exe /select,"C:\Temp\Myfile.png"
我直接在命令提示符中运行该命令以验证其有效性,确实可以。但是,我无法在Node中以良好的方式运行它。我尝试了一些方法:
const expl = exec('cmd.exe', ["explorer.exe", `/select,"${root}\\${filename}\"`]);
const expl = spawn('cmd.exe', ["explorer.exe", `/select,"${root}\\${filename}\"`]);
const expl = exec('cmd.exe', [`explorer.exe /select,"${root}\\${filename}\"`]);
...以及其他一些变化。我并不真正知道自己在做什么。
最终,我写了一个非常丑陋的解决方案:
function openExplorerSelected(filename){
let batfile = `explorer.exe /select,\"${root}\\${filename}\"`;
fs.writeFile("tmp.bat", batfile, function(err){
if( err ) console.warn(err);
else {
const expl = spawn('cmd.exe', ["/c", "tmp.bat"]);
}
})
}
它能够工作,但感觉很糟糕。
应该如何正确地完成这个任务?