在文件资源管理器中打开一个目录

23

在ms Windows平台上,如何使用node.js代码打开Windows文件资源管理器中的特定目录(例如:c:\documents)?

我猜在c#中,可以这样写:

process.Start(@"c:\test")

在实际的命令行调用中,输入“start c:\test”以启动命令……在 shell 中只输入“c:\test”不会产生任何效果…… - Marc B
这里有一个在 Windows/MacOS/Linux 上都能够 完美运行 的示例:https://stackoverflow.com/a/68010888/12666332 - SeyyedKhandon
5个回答

45

请尝试以下操作,它会在运行Node.js的计算机上打开文件资源管理器窗口

require('child_process').exec('start "" "c:\\test"');

如果你的路径不包含空格,你也可以通过使用 'start c:\\test' 来实现,但上述方法是最健壮的,因为它需要将 "" 作为第二个参数[1]

注意:

  • 文件资源管理器窗口将异步启动,并在启动时获得焦点。

  • 这个相关问题 请求一个防止窗口“抢占”焦点的解决方案。


[1] 默认情况下,cmd.exe 的内部start命令将以 "..." 包含的第一个参数解释为新控制台窗口的窗口标题(这里不适用)。通过明确提供(虚拟的)窗口标题-"",第二个参数可被可靠地解释为目标可执行文件/文档路径。


注意建议。谢谢@mklement0我正在尝试开发一个浏览共享网络文件夹的Web应用程序。是否可能通过托管在另一台机器上的Web应用程序仅发出打开命令来在本地机器上打开文件夹:require('child_process').exec('start "" "E:\test"');其他计算机可以在局域网或互联网中。我将从我的本地机器访问该应用程序。 基本上,我将实现一个导航菜单,以快速导航到我们服务器中所需的文件夹。稍后,我将添加一些代码来检查哪些文件夹为空。 - Abdullah
@Abdullah:这不是我的专业领域,但我怀疑浏览器安全限制(沙箱)将是一个问题。我建议你问一个单独的后续问题(链接到这个问题),提供更多细节,并展示你到了哪一步。还要注意,如果有许可的话,这将只能是_本地_ JavaScript,由_打开文件夹的_浏览器运行 - 没有涉及Node.js(除非你设法调用_本地_ 的 'node' 命令)。 - mklement0
1
这将仅在运行节点服务的PC上打开一个目录,而不会在连接到它的客户端上打开。 - xinthose

7

使用这个包会很有益,因为它可以在不同的平台上打开文件资源管理器。 https://www.npmjs.com/package/open-file-explorer

或者只使用其中的一部分。

function openExplorerin(path, callback) {
    var cmd = ``;
    switch (require(`os`).platform().toLowerCase().replace(/[0-9]/g, ``).replace(`darwin`, `macos`)) {
        case `win`:
            path = path || '=';
            cmd = `explorer`;
            break;
        case `linux`:
            path = path || '/';
            cmd = `xdg-open`;
            break;
        case `macos`:
            path = path || '/';
            cmd = `open`;
            break;
    }
    let p = require(`child_process`).spawn(cmd, [path]);
    p.on('error', (err) => {
        p.kill();
        return callback(err);
    });
}

3

我在WSL和可能的Windows系统中找到了另一种方法。

请注意,您必须确保为Windows而不是Linux(WSL)格式化路径。 我想要在Windows上保存一些东西,为此你需要使用WSL上的/mnt目录。

// format the path, so Windows isn't mad at us
// first we specify that we want the path to be compatible with windows style
// then we replace the /mnt/c/ with the format that windows explorer accepts
// the path would look like `c:\\Users\some\folder` after this line
const winPath = path.win32.resolve(dir).replace('\\mnt\\c\\', 'c:\\\\');

// then we use the same logic as the previous answer but change it up a bit
// do remember about the "" if you have spaces in your name
require('child_process').exec(`explorer.exe "${winPath}"`);

这应该为您打开文件浏览器。


1
一种稍微简单一些、更跨平台的解决方案是使用这个。
let explorer;
switch (platform()) {
    case "win32": explorer = "explorer"; break;
    case "linux": explorer = "xdg-open"; break;
    case "darwin": explorer = "open"; break;
}
spawn(explorer, [path], { detached: true }).unref();

platform()来自于os模块,而spawn来自于child_process模块。


0
您可以使用explorer-opener软件包:
https://www.npmjs.com/package/explorer-opener
import { openExplorer } from 'explorer-opener';

openExplorer('C:\\Windows\\System32')
  .then(() => {
    // handle successful open
  })
  .catch((error) => {
    // handle error
  });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接