ReadKey 在 .net 核心中不起作用 [英] ReadKey not working in .net core

查看:23
本文介绍了ReadKey 在 .net 核心中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ReadKey() 来获取密码字段,因此它不会显示在我在 Ubuntu 上运行的 .net 核心应用程序的控制台中.

I am trying to use ReadKey() to get a password field so it doesn't show in console for a .net core app I am running on Ubuntu.

这是代码:

        while (true)

        {

            var key = System.Console.ReadKey(true);

            if (key.Key == ConsoleKey.Enter)

                break;

            Io.RootPass += key.KeyChar;

        } 

但我得到这个例外:

发生异常:CLR/System.InvalidOperationExceptionSystem.Console.dll 中发生类型为System.InvalidOperationException"的未处理异常:当任一应用程序没有控制台或控制台输入已被重定向时,无法读取密钥.试试 Console.Read.'

Exception has occurred: CLR/System.InvalidOperationException An unhandled exception of type 'System.InvalidOperationException' occurred in System.Console.dll: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

ReadLine() 工作正常.为什么 ReadKey() 不起作用?

ReadLine() works fine. Why doesn't ReadKey() work?

实际上 Console.ReadLine() 不起作用,或者它只是不抛出异常.回车时没有分配返回值,所以程序会卡住.

Actually Console.ReadLine() doesn't work either it just doesn't throw an exception. The return value is not assigned when enter hit so program sticks.

我猜这是因为 linux 终端的工作方式不同.是否有适用于 Linux 终端的不同界面?

I am guessing this is because linux terminal works differently. Is there a different interface for linux terminal that works?

推荐答案

在 OmniSharp Visual Code 文档中找到了我的答案:https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

Found my answer in OmniSharp Visual Code docs: https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

控制台(终端)窗口

默认情况下,进程以其控制台输出启动(stdout/stderr) 进入 VS Code 调试器控制台.这很有用对于从网络、文件等获取输入的可执行文件.但这不适用于想要从控制台(例如:Console.ReadLine).对于这些应用程序,请使用设置例如以下:

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" 当设置为integratedTerminal目标进程将在 VS Code 的集成终端内运行.单击编辑器下方选项卡组中的终端"选项卡以与您的应用互动.

"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.

当它设置为 externalTerminal 时,目标进程将在一个单独的终端.

When this is set to externalTerminal the target process will run in a separate terminal.

我在 launch.json 中更改了此设置,现在可以使用了

I changed this setting in launch.json and now it works

这篇关于ReadKey 在 .net 核心中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆