调试控制台窗口在调试期间无法接受 Console.ReadLine() 输入 [英] Debug Console window cannot accept Console.ReadLine() input during debugging

查看:51
本文介绍了调试控制台窗口在调试期间无法接受 Console.ReadLine() 输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VSCode 版本:1.8.0

VSCode Version: 1.8.0

操作系统版本:Win10 x64

OS Version: Win10 x64

重现步骤:

  1. 使用dotnet new"创建一个新的 .net core cli 应用
  2. 使用 VS 代码打开文件夹
  3. 在 Program.cs 中添加两行代码

  1. Create a new .net core cli app using "dotnet new"
  2. Open the folder using VS code
  3. Add two lines of code in Program.cs

string a = Console.ReadLine();Console.WriteLine(a);

string a = Console.ReadLine(); Console.WriteLine(a);

切换到 VS code 调试窗口并开始调试,Debug Console 窗口显示,并显示第一个Hello, World".输出,停在Console.ReadLine()这一行,在调试控制台输入任何东西,按回车会报错无法执行此操作,因为进程正在运行."

Switch to VS code debug window and start debugging, Debug Console window shows, and displays the first "Hello, World." output, and stops on the line of Console.ReadLine(), enter anything in the Debug Console and press Enter will be given err message of "Unable to perform this action because the process is running."

问题是在调试期间如何以及在何处为 Console.ReadLine() 输入文本以接受,如果我打开一个新的 cmd.exe 并执行dotnet 运行"它工作正常,但在 Visual Studio Code 调试控制台中它是不工作.

The question is how and where to enter text for Console.ReadLine() to accept during debugging, if I open a new cmd.exe and do a "dotnet run" it works fine, but in Visual Studio Code Debug Console it's not working.

推荐答案

要在调试时读取输入,您可以在 launch.json 的配置中使用 console 属性

To read input whilst debugging, you can use the console property in your configurations in launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "program": "${workspaceFolder}/bin/Debug/net5.0/your-project-name.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "console": "integratedTerminal"
        }
    ]
}

您可以使用 externalTerminal"integratedTerminal".internalConsole" 似乎不起作用.

You can either use "externalTerminal" or "integratedTerminal". The "internalConsole" doesn't appear to be working.

我使用 integratedTerminal 设置,因为终端在 VSCode 内部.您现在可以使用 Console.ReadLine();

I use the integratedTerminal setting, as the terminal is inside VSCode itself. You will now be able to read input with Console.ReadLine();

注意:另外,internalConsole 不起作用,这是设计使然.这是因为 internalConsole 使用调试控制台选项卡来显示 Console.WriteLine 的输出.由于调试控制台中的输入框用于在当前堆栈上运行表达式,因此没有地方可以传入将进入 Console.ReadLine 的输入.这就是您必须使用 integratedTerminal 之类的原因.

Note: Also, internalConsole doesn't work, and it is by design. The reason this is, is because internalConsole uses the Debug Console tab to show the output of the Console.WriteLine. Since the input box in the Debug Console is used to run expression on the current stack, there's no place to pass in input that will go to Console.ReadLine. That's the reason you'll have to use something like integratedTerminal.

下面的截图表明VSCode团队知道这一点-

The screenshot below shows that the VSCode team knows this -

这篇关于调试控制台窗口在调试期间无法接受 Console.ReadLine() 输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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