如何在Visual Studio Code中使用Console.ReadLine


背景

目前,我正在Mac上进行培训。但是,我喜欢C#,所以我使用Unity。在第一个语法研究中,我使用MonoDevelop开发了一个控制台应用程序。
但是,从Unity 2018开始,它将是Visual Studio for Mac而不是Mono Develop。但是,此后有时在Atom的PHP和Ruby培训中使用,因此我决定在Visual Studio Code(以下简称VS Code)中尝试使用Atom。

问题

我写了一个代码来输出

字符并读取它们。

1
2
3
4
5
static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string messageg = Console.ReadLine();
        }

输出如下所示,但是程序在输入部分停止。

VS代码スクリーンショット 2018-10-04 16.46.38.png

我检查发现VS Code调试控制台仅用于输出,不能输入。

解决方案

因此,我更改了设置以在普通终端中运行。
在.vscode目录下打开launch.json。

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/consoleTest.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
//この下の部分を変更
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },

此"控制台":" internalConsole"部分是
重写为"控制台":" externalTerminal"。

launch.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.1/consoleTest.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
//この下の部分を変更!!
            "console": "externalTerminal",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },

在执行此操作时我注意到了,但是当我用鼠标指向"内部控制台"部分时,

internalConsole:输出到VS Code调试控制台。这不支持读取控制台输入(例如:Console.ReadLine)

它显示为

。换句话说,"如果将控制台设置设置为" internalConsole",则无法在VS代码调试控制台中使用控制台(例如Console.ReadLine)的输入"。
重写后,终端将在成功执行时打开,现在可以输入了。

我提到了以下站点,但是设置项略有不同。

调试控制台窗口在调试期间不能接受Console.ReadLine()输入