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

时间:2016-01-28 21:29:27

标签: node.js windows

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

我想在 c#中,它会是:

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

2 个答案:

答案 0 :(得分:14)

尝试以下方法:

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

注意:如果您的路径不包含空格,您也可以使用'start c:\test',但上述内容 - 需要""作为第二个参数,并且\个实例加倍path(3rd)参数 - 是最强大的方法。

答案 1 :(得分:1)

我在 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}"`);

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