从控制台在文件资源管理器中打开一个目录并将焦点返回到控制台 [英] Open a directory in File Explorer from the console and return focus back to the console

查看:74
本文介绍了从控制台在文件资源管理器中打开一个目录并将焦点返回到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我可以在 Windows 10 的资源管理器中打开一个文件夹:

Using the following code I can open a folder in Explorer in Windows 10:

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

这很好用.但是,焦点将放在新打开的窗口上.我正在从节点脚本中使用它,并在窗口打开后提示用户输入.这对用户来说很烦人,因为他们必须在控制台窗口内手动单击才能响应输入请求.

This works well. However, focus is given to the newly opened window. I'm using this from a node script and am prompting for input from the user after the window has been opened. This is annoying to the user as they have to manually click inside the console window to respond to the input request.

有什么办法可以修改上面的命令,要么首先不将焦点放在打开的窗口上.或者,强制焦点返回到控制台窗口?

Is there any way that the above command can be modified to either not give focus to the opened window in the first place. Or, force focus to return to the console window?

推荐答案

注意:

  • 这个答案即使在 Git Bash 中也应该有效.

  • This answer should work even from Git Bash.

由于代码涉及创建 PowerShell 进程和涉及 C# 代码按需编译的代码,解决方案很慢.

Since the code involves creating a PowerShell process and code that involves on-demand compilation of C# code, the solution is slow.

文件资源管理器窗口短暂获得焦点,但先前活动的窗口会立即重新激活.

The File Explorer window briefly receives focus, but the previously active window is reactivated right away.

虽然 Shell.Application COM 对象的 .ShellExcecute() 方法将提供更简单、更快速的解决方案,但它能够在不激活窗口的情况下启动进程不幸的是,它不适用于 explorer.exe.

While the Shell.Application COM object's .ShellExcecute() method would make for a simpler and faster solution, its ability to launch a process without window activation unfortunately doesn't work with explorer.exe.

require('child_process').execFile(
  'powershell.exe', 
  [ 
    '-NoProfile',
    '-Command', 
    `
    $type = Add-Type -PassThru -Namespace Util -Name WinApi -MemberDefinition '[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();'
    # Get the current foreground window.
    $hwndBefore = $type::GetForegroundWindow()
    # Open the target folder in File Explorer
    Start-Process 'c:\\temp'
    # Wait for a different window - assumed to be the File Explorer window - to become active.
    while ($hwndBefore -eq $type::GetForegroundWindow()) { Start-Sleep -Milliseconds 200 }
    Start-Sleep -Milliseconds 200
    # Put focus back on the previous foreground window.
    $null = $type::SetForegroundWindow($hwndBefore)
    `
  ]
 );

注意:我在上面使用了 ES6 模板字面量 (`...`),以便能够定义多行用它串起来;从概念上讲,上面是一个 verbatim 字符串.当心 PowerShell 构造,例如模板文字中的 ${var},因为它们会被解释为 JavaScript 表达式以进行扩展.

Note: I've used an ES6 template literal (`...`) above, for the convenience of being able to define multi-line strings with it; conceptually, the above is a verbatim string. Beware of PowerShell constructs such as ${var} inside a template literal, because they would be interpreted as JavaScript expressions to expand, up front.

这篇关于从控制台在文件资源管理器中打开一个目录并将焦点返回到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆