简体   繁体   English

使用批处理文件打开vscode时,cmd打开不关闭

[英]When opening vscode using batch file, cmd opens and doesn't close

When trying to open vscode folder using a batch file, Visual Studio opens up with that folder, but also a cmd window pops up and does not go away if you use exit command.尝试使用批处理文件打开 vscode 文件夹时,Visual Studio 会打开该文件夹,但也会弹出一个 cmd 窗口,如果您使用 exit 命令,该窗口不会消失。

@echo off
start code "C:\GitHub\TestApp\testapp"
exit 

VSCode opens up correctly, but also this window opens VSCode 正确打开,但此窗口也会打开

cmd窗口

Using VSCode 1.52.1, the only way I could start it without having a cmd window open after exiting the batch script is:使用 VSCode 1.52.1,我可以在退出批处理脚本后不打开 cmd 窗口的情况下启动它的唯一方法是:

explorer.exe "%userprofile%\AppData\Local\Programs\Microsoft VS Code\Code.exe"

Note: it does not involve opening a specific local directory to work with.注意:它不涉及打开要使用的特定本地目录。 But maybe you can find a solution such as saving the folder as a workspace or using Ctrl + R to open recent folders.但是也许您可以找到解决方案,例如将文件夹保存为工作区或使用Ctrl + R打开最近的文件夹。 Plus, if you work only within that directory / workspace, or use it right before closing VSCode, it will be opened automatically at the next launch.另外,如果您只在该目录/工作区中工作,或者在关闭 VSCode 之前使用它,它将在下次启动时自动打开。

That's because you are actually invoking the batch file code.cmd which is located at [VSCodePath]\\bin\\code.cmd .那是因为你实际上是在调用批处理文件code.cmd其位于[VSCodePath]\\bin\\code.cmd The code.cmd file in turn invokes the actual VSCode executable code.exe code.cmd文件依次调用实际的 VSCode 可执行文件code.exe

When invoking a batch file ( .BAT or .CMD ) using the start command, a new instance of CMD process will be created to handle the execution of the batch file, But it invokes the CMD process with the /K switch rather than /C使用start命令调用批处理文件( .BAT.CMDstart ,将创建 CMD 进程的新实例来处理批处理文件的执行,但它使用/K开关而不是/C调用 CMD 进程

For example start code.cmd executes cmd /k code.cmd比如start code.cmd执行cmd /k code.cmd

It is the /K switch that causes the new cmd to remain open after finishing the execution of the batch file.正是/K开关使新的 cmd 在完成批处理文件的执行后保持打开状态。

To resolve, instead of supplying the batch file directly the to the start command, execute it by an explicit CMD invokation:要解决此问题,请不要直接将批处理文件提供给start命令,而是通过显式 CMD 调用执行它:

@echo off
start cmd /C code "C:\GitHub\TestApp\testapp"
exit 

That CMD window is associated with the VSCode instance that you just opened.该 CMD 窗口与您刚刚打开的 VSCode 实例相关联。 Attempting to close it will terminate the application you started.尝试关闭它将终止您启动的应用程序。 ( in this case, VSCode ) 在这种情况下, VSCode

The start xxx xxx... command opens up a new cmd terminal to perform its action. start xxx xxx...命令打开一个新的 cmd 终端来执行它的操作。 Even though a new prompt appears, which can be used as a normal terminal itself, the VSCode process is inexorably linked to it as the parent process.即使出现了一个新的提示,它本身可以用作普通终端,但 VSCode 进程作为父进程不可阻挡地链接到它。

If your goal is to not launch a separate cmd window, then run:如果您的目标是不启动单独的 cmd 窗口,请运行:

start code /b "C:\\GitHub\\TestApp\\testapp"

which just runs the command in the same window.它只是在同一窗口中运行命令。 The VSCode window is still inexorably bound to the current cmd window and will close if the cmd window disappears, but at least another cmd window isn't launched. VSCode 窗口仍然不可避免地绑定到当前 cmd 窗口,如果 cmd 窗口消失,它将关闭,但至少没有启动另一个 cmd 窗口。

Windows doesn't have the capability to launch a program in the background from the terminal. Windows 无法从终端在后台启动程序。

If all described solutions did not work for you, try making an ordinary Windows shortcut to "C:\\Users\\username\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe" C:\\path-to-project-folder-or-file .如果所有描述的解决方案都不适合您,请尝试创建一个普通的 Windows 快捷方式到"C:\\Users\\username\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe" C:\\path-to-project-folder-or-file

快捷方式属性窗口

Then call this shortcut in your .bat or .cmd script like that (assuming shortcut name is shortcut ):然后在你的.bat.cmd脚本中调用这个快捷方式(假设快捷方式名称是shortcut ):

@echo off
start C:\path-to-shortcut-file\shortcut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用批处理文件时如何关闭cmd窗口? - How to close cmd window when using a batch file? 打开adobe reader打印PDF的批处理文件不会结束/关闭 - Batch file that opens adobe reader to print PDF doesn't end/close 使用批处理文件在 VSCode 中打开项目 - Opening project in VSCode using batch file 打开一个批处理文件,但是当它打开时,我的python代码停止了,因为该批处理文件在同一窗口中打开 - opening a batch file but when it opens my python code is stopped because the batch file opens in the same window 当我打开一个批处理文件时,它应该使用 python 创建另一个文件,python 打开它,但它不会创建文件 - When I open a batch File that is supossed to create another file with python, python opens it but it doesn't create the file 运行批处理文件后,CMD没有关闭,删除文件后文件没有复制? - CMD doesn't close after I run a batch file and files don't copy after deleting them once? 如何在批处理文件中关闭cmd - How to close cmd in batch file 批处理文件执行后,为什么CMD窗口没有关闭? - Why won't the CMD window close after Batch file execution? 在Windows中使用Python打开文件后如何关闭cmd? - How to close cmd after opening a file using Python in Windows? START后批处理文件未关闭 - Batch file doesn't close after START
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM