本体java猿,近期在做各种拓展
在尝试使用Console.ReadLine()和Console.ReadKey()的操作,结果在调试的时候发生了一些问题。
使用readLine是ok的,但是使用readKey会丢出Exception。
发生异常: CLR/System.InvalidOperationException
“System.InvalidOperationException”类型的未经处理的异常在 System.Console.dll 中发生 : 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'
在OmniSharp Visual Code docs里找到了答案:
Console (terminal) window
By default, processes are launched with their console output (stdout/stderr) going to the VS Code Debugger Console. This is useful for executables that take their input from the network, files, etc. But this does NOT work for applications that want to read from the console (ex: Console.ReadLine). For these applications, use a setting such as the following:
"console": "integratedTerminal" When this is set to integratedTerminal the target process will run inside VS Code's integrated terminal. Click the 'Terminal' tab in the tab group beneath the editor to interact with your application.
When this is set to externalTerminal the target process will run in a separate terminal.
将在launch.json文件中,将consle改为“integratedTerminal
”即可
"console": "integratedTerminal"
"console": "internalConsole"
改成"console": "integratedTerminal"
即可运行成功。
参考文章:https://blog.csdn.net/m0_45338067/article/details/115006674
但是之后还是出现了一个问题
static void Main(string[] args)
{
String input = Console.ReadLine();
Console.WriteLine(input);
Console.ReadKey();
}
在我测试读写的时候,vscode会在ReadLine卡住并不会往下运行了
但是在debug中却可以正常显示
猜测有可能是vscode控制台交互的问题