栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号