关于C#中Console.ReadKey()引发的问题

本体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控制台交互的问题

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值