【问题标题】:Opening Explorer to a specific file in Node.js在 Node.js 中打开资源管理器到特定文件
【发布时间】:2016-07-19 11:26:08
【问题描述】:

我查看了一些相关问题,并且存在将explorer.exe 打开到特定文件的方法,以便在资源管理器窗口中选择它。命令行是这样的:

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

    }
  })
}

它有效,但是。

这样做的正确方法是什么?

【问题讨论】:

    标签: javascript node.js fs


    【解决方案1】:

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

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

    【讨论】:

    • 这会打开资源管理器,但不会打开正确的路径(更不用说选择的文件)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 2014-03-31
    • 1970-01-01
    相关资源
    最近更新 更多