简体   繁体   English

将Explorer打开到Node.js中的特定文件

[英]Opening Explorer to a specific file in Node.js

I've looked at some related questions, and there exist ways of opening explorer.exe to a specific file, such that it is selected in the Explorer window. 我已经查看了一些相关的问题,并且存在将explorer.exe打开到特定文件的方法,以便在Explorer窗口中选择它。 The command line goes something like this: 命令行是这样的:

explorer.exe /select,"C:\\Temp\\Myfile.png"

I ran that command in the command prompt directly to verify that it works, and it does. 我直接在命令提示符中运行该命令以验证它是否有效,并且确实如此。 However, I cannot get it to run in Node in a good way. 但是,我不能让它以一种好的方式在Node中运行。 Some things I've tried: 我试过的一些事情:

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}\"`]);

...and some other variations. ......以及其他一些变化。 I didn't / don't really know what I am doing. 我没有/不知道我在做什么。

I ended up writing a truly ugly solution: 我最终写了一个真正丑陋的解决方案:

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"]);

    }
  })
}

It works, but ew. 它有效,但是。

What is the right way of doing this? 这样做的正确方法是什么?

您是否尝试将explorer.exe作为可执行文件而不是cmd.exe运行?

const expl = exec('explorer.exe', [`/select,"${root}\\${filename}\"`]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM