windowsvisual-studio-codecmd

Why is cmd with switch /c not working from File Explorer address bar as expected with closing console window after start of VS Code?


I use the Explorer address bar to directly open the folder in VS Code.

I first open cmd by typing cmd in address bar from the directory I want to open in Visual Studio Code. I have been trying to find out the direct way.

I tried using cmd /c code . as well as cmd /c "code ." That opens up my folder in VS Code but the problem is, cmd doesn't close.


Solution

  • The usage of cmd.exe is not necessary at all. There can be run directly %LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe with the current directory path as argument as it can be read on the Visual Studio Code command-line interface page.

    The used method results in explorer.exe calling the Windows kernel library function CreateProcess for running cmd.exe with the three arguments /c and code and ..

    The help output on running cmd /? in a command prompt window explains that the option /c instructs the Windows Command Processor to parse and run the command line as specified next with the remaining arguments and then close itself.

    code is the name of the executable file to run without path and without file extension.

    . references the current working directory of cmd.exe in this case. See the Microsoft documentation about Naming Files, Paths, and Namespaces which explains also . as relative path for referencing the current directory.

    The Windows Command Processor calls for that reason CreateProcess for execution of Code.exe after having found it using the environment variables PATH and PATHEXT as described by this answer in full details with . as argument for Code.exe and waits with self-closing until started Code.exe closes. That waiting is not wanted here. The solution is not using completely unnecessary cmd.exe at all just for doing what explorer.exe could already do: Call CreateProcess for running Code.exe with a directory path as argument.

    NOTE: The description below expects a Visual Studio Code installation into the directory %LOCALAPPDATA%\Programs\Microsoft VS Code which is the default installation directory. Replace this directory path with the installation directory path of Visual Studio Code if the installation was done into a different directory.

    Using a shortcut file in the Send to folder/context menu

    One solution is using a shortcut file in the Send to folder/context menu for starting Visual Studio Code with a directory path or a file name as also possible which can be configured as follows.

    Start Windows File Explorer and browse to directory %LOCALAPPDATA%\Programs\Microsoft VS Code. That can be done also by selecting this directory path here in the web browser, copy it with Ctrl+C to the clipboard, click into the address bar of the Windows File Explorer, paste the path with Ctrl+V and press key RETURN or ENTER to open this directory.

    Click with secondary (usually right) pointing device (mouse) button on the file Code.exe to open the context menu for the executable file of Visual Studio Code.

    Click with primary (usually left) pointing device button on the item Show more options on Windows 11 and click next on any Windows in submenu Send to on the context menu item Desktop (create shortcut). Then minimize the Windows File Explorer window to see your desktop with the new shortcut file with name Code.exe or Code.exe - Shortcut depending on version of Windows. There can be also pressed the key combination Win+D (Windows logo key pressed and hold plus key D) for showing the desktop. Win+D pressed once again restores the application windows. See also the Microsoft documentation page Keyboard shortcuts in Windows.

    Click with secondary pointing device button on the newly created shortcut file and (after clicking on Show more options on Windows 11) click on context menu item Rename to change the name of the shortcut file to something meaningful like Open in VS Code. The name of the shortcut file is later the name of a context menu item in context submenu Send to.

    Clicked with secondary pointing device button on the newly created and already renamed shortcut file and click on the context menu item Properties for opening the Properties dialog window for the shortcut.

    The property Target is passed with function parameter lpCommandLine to CreateProcess by explorer.exe on later using the shortcut. It could be modified by appending options like --new-window (with a preceding space). Important to know is that explorer.exe appends on later usage of the shortcut a space and the fully qualified directory/file name of the directory/file currently selected in Windows File Explorer to the command line as defined with property Target.

    The directory path specified with property Start in is passed with function parameter lpCurrentDirectory to CreateProcess and defines the current working directory for the started executable Code.exe. In this case it is best to have defined Start in in the shortcut properties with no path. Then the current working directory of explorer.exe will be also the current working directory of Visual Studio Code. But there can be used also the program files directory of Visual Studio Code as Start in.

    The property Comment should be configured with a useful hint like Open a directory or file in Visual Studio Code.

    There can be selected a nice icon displayed later in the context submenu Send to and of course all other properties can be customized too. They all define how explorer.exe calls CreateProcess on using the shortcut. Finally click on button OK for saving all changed properties and closing the window.

    Cut the modified shortcut file with Ctrl+X from desktop, restore the Windows File Explorer window, browse to the directory %APPDATA%\Microsoft\Windows\SendTo and paste with Ctrl+V the shortcut file.

    There can be opened now in Windows File Explorer (or any other file manager with Explorer context menu support) the context menu of a directory or file with secondary pointing device button and (after clicking on Show more options on Windows 11) clicked in submenu Send to on the context menu item with name of the shortcut file like Open in VS Code. Then explorer.exe (or any other file manager executable) starts Visual Studio Code with the fully qualified name (drive + path + name + extension) of the directory/file currently selected in file manager appended as additional argument.

    The Send to context menu item Open in VS Code can be removed after uninstalling Visual Studio Code by deleting the shortcut with that file name in the directory %APPDATA%\Microsoft\Windows\SendTo.

    Using a registered open context menu item for directories

    Another solution is registering an Open in VS Code context menu item for directories in the Windows registry to have directly in the context menu of a directory/folder the context menu item Open in VS Code. This can be done from within a command prompt window by running the following commands:

    reg.exe ADD "HKCU\Software\Classes\Directory\shell\Open in VS Code" /v Icon /d "%LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe,0" /f
    reg.exe ADD "HKCU\Software\Classes\Directory\shell\Open in VS Code" /v MultiSelectModel /d Single /f
    reg.exe ADD "HKCU\Software\Classes\Directory\shell\Open in VS Code\command" /ve /d ^"\"%LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe\" \"%1\"^" /f
    

    See the Microsoft documentation page Creating Shortcut Menu Handlers for more details. Run in a command prompt window reg.exe QUERY HKCR\Directory /s for getting output all existing registered shell actions and context menu handlers effective for the current user.

    Clicking now in Windows File Explorer or any other file manager with support for registered Windows shell extensions and context menu handlers on a directory with the secondary pointing device button opens the context menu for the clicked directory containing (after clicking on Show more options on Windows 11) the context menu item Open in VS Code.

    The first icon in the icon resources of Code.exe of Visual Studio Code is used here as icon to display in the context menu. There can be also registered a different icon from the icon resources of Code.exe by changing the number after the comma or any other file containing an icon resource.

    The registry string value with name MultiSelectModel and with value Single makes sure to get the context menu option Open in VS Code displayed only if a single directory is selected in the file manager. This context menu option is not displayed on having multiple directories selected in file manager as it is not possible to start Visual Studio Code with multiple directory arguments. That is an advantage of this solution in comparison to the Send to shortcut solution as an execution of Visual Studio Code by mistake with multiple selected directories is not possible now.

    %1 is replaced by the fully qualified name of the clicked directory.

    There can be also VS Code options like --new-window inserted in the command line left to \"%1\" on registering the command for Open in VS Code.

    There can be executed the following command in a command prompt window after uninstalling of Visual Studio Code to remove the context menu item Open in VS Code from the registry:

    reg.exe DELETE "HKCU\Software\Classes\Directory\shell\Open in VS Code"
    

    Additional information regarding to shell registrations

    The Windows shell is the application which is showing the user after sign in/log in the Windows desktop, the Windows start button/menu, and the Windows taskbar with the system tray. There is stored in Windows registry under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon the registry string value with name Shell with string value explorer.exe as default. The first started explorer.exe after a user logged on is running for that reason by default as Windows shell. There could be used also a different executable as Windows shell. But that executable must support everything which is supported by explorer.exe for working as Windows shell.

    I want to add also information about the difference between Directory and Folder in HKEY_CLASSES_ROOT which is a merged view of 64-bit and 32-bit HKEY_CURRENT_USER\Software\Classes and HKEY_LOCAL_MACHINE\Software\Classes. See also the Microsoft documentation page Registry Keys Affected by WOW64 for more details.

    There can be executed in a command prompt window reg.exe QUERY HKCR\Folder /s for getting output the Folder registration as effective for the current user.

    Directory is just for real directories in a file system.

    Folder includes real directories in a file system as well as virtual folders like Home or Gallery and others displayed by the Windows File Explorer. Home is not a real directory in file system. It is a virtual folder with the identifier {F874310E-B6B7-47DC-BC84-B9E6B38F5903}.

    A registration of Open in VS Code under key Folder instead of Directory would result in this context menu item displayed also in the context menu of Home with execution of Visual Studio Code with "::{F874310E-B6B7-47DC-BC84-B9E6B38F5903}" as argument on clicking on the context menu item. That would be of course not useful as Visual Studio Code does not know what to do with this argument string. That is the reason for adding Open in VS Code to Directory and not to Folder.