LoginSignup
20
19

More than 5 years have passed since last update.

Visual Studio Code で Console.ReadLineを使う方法

Last updated at Posted at 2018-10-04

経緯

現在、Macで研修を行っています。とはいえC#大好きなので,Unityを使っていました。最初の構文の勉強ではMonoDevelopでコンソールアプリを作っています。
ところがUnity2018よりMonoDevelopではなく、Visual Studio for Macになるとのこと。でもその後のPHPやRubyの研修でAtom使うこともあり、いっそのことVisual Studio Code(以下 VS Code)でやってみようと思いました。

問題点

文字を出力し、文字を読み込むコードを書いて見ました。

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

下図のようになり出力はできるのですが、入力部分でプログラムが停止してしまいます。

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

調べたところVS Codeのデバッグコンソールは出力専用で入力はできないとのことでした。

解決

それで通常のターミナルで実行するように設定を変更しました。
.vscodeディレクトリ配下にあるlaunch.jsonを開きます。

launch.json

   "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"
        },

この"console":"internalConsole"の部分を
"console":"externalTerminal"に書き換えました。

launch.json
   "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"の部分をマウスでポイントすると

internalConsole:Output to the VS Code Debug Console.This doesn't support reading console input (ex:Console.ReadLine)

と表示されました。意訳すると、「consoleの設定値を"internalConsole”にすると、例えばConsole.ReadLineみたいなコンソールからの入力はVS codeのデバッグコンソールでは使えないよ」となり思いっきり当てはまってました。
書き換えたところ無事実行時にターミナルが開き、入力も行えるようになりました。

参考にしたのは下記サイトですが、微妙に設定項目が違いました。

Debug Console window cannot accept Console.ReadLine() input during debugging

20
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
19