【发布时间】:2016-01-28 21:29:27
【问题描述】:
在来自 node.js 代码的 ms Windows 上,如何在 Windows 文件资源管理器中打开特定目录(例如:c:\documents)?
我猜在 c# 中应该是:
process.Start(@"c:\test")
【问题讨论】:
-
start c:\test,用于实际的命令行调用...c:\test本身不会在 shell 中执行任何操作...
在来自 node.js 代码的 ms Windows 上,如何在 Windows 文件资源管理器中打开特定目录(例如:c:\documents)?
我猜在 c# 中应该是:
process.Start(@"c:\test")
【问题讨论】:
start c:\test,用于实际的命令行调用...c:\test 本身不会在 shell 中执行任何操作...
尝试以下操作,在运行 Node.js 的计算机上打开一个文件资源管理器窗口:
require('child_process').exec('start "" "c:\\test"');
如果您的路径不包含空格,您也可以使用 'start c:\\test',但上述方法 - 需要 "" 作为第二个参数[1] 是最可靠的方法.
注意:
文件资源管理器窗口将异步启动,并在启动时接收焦点。
This related question 要求提供一种防止窗口“窃取”焦点的解决方案。
[1] 默认情况下,cmd.exe 的内部 start 命令将 "..." 封闭的第一个参数解释为要创建的新控制台窗口的 窗口标题(其中不适用于此处)。通过提供(虚拟)窗口标题 - "" - 明确地,第二个参数被可靠地解释为目标可执行文件/文档路径。
【讨论】:
node命令。
我在 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}"`);
这应该会为您打开文件资源管理器。
【讨论】: