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

查看:976
本文介绍了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;

        } 

但是我得到了这个例外:

But I get this exception:


发生了异常:CLR / System.InvalidOperationException
System.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终端工作方式不同。

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


控制台(终端)窗口

Console (terminal) window

默认情况下,进程以其控制台输出
(stdout / stderr)进入VS Code Debugger Console的方式启动。
对于从网络,文件等获取其输入的可执行文件很有用。
但这不适用于希望从
控制台读取的应用程序(例如: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:

控制台: 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天全站免登陆