gpt4 book ai didi

c# - ReadKey InvalidOperationException 应用程序没有控制台

转载 作者:太空狗 更新时间:2023-10-29 21:04:01 28 4
gpt4 key购买 nike

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

这是代码:

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

if (key.Key == ConsoleKey.Enter)
break;

Io.RootPass += key.KeyChar;
}

但是我得到了这个异常(exception):

Exception has occurred: CLR/System.InvalidOperationExceptionAn 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.'

Console.ReadLine() 不起作用,它只是不抛出异常。输入时不会分配返回值,因此程序会卡住。

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

谢谢!

最佳答案

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

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 中更改了此设置,现在可以使用了

关于c# - ReadKey InvalidOperationException 应用程序没有控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901071/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com