34

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

This is code:

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

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

    Io.RootPass += key.KeyChar;
} 

But I get this exception:

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

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

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

Thanks!

spaleet
  • 838
  • 2
  • 10
  • 23
Guerrilla
  • 13,375
  • 31
  • 109
  • 210

3 Answers3

57

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

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.

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

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • 3
    I have no launch.json file, where is this located? – Spartaok Nov 14 '18 at 10:52
  • 1
    Its created by vscode when you click debug settings. – Guerrilla Nov 14 '18 at 12:47
  • Could you please check that: https://stackoverflow.com/questions/53300317/how-to-create-a-navigation-menu-in-dotnet-application – Spartaok Nov 14 '18 at 13:03
  • I just went back and checked my code to see what I did as this was over a year ago. Looks like I refactored app to work from command line arguments and settings file – Guerrilla Nov 14 '18 at 14:25
  • could you show me the way in my question? I'll be grateful – Spartaok Nov 14 '18 at 15:24
  • For anyone interested, an external terminal can also debug, which works better and less clunky than the VS Code integrated terminal : `"console": "externalTerminal"` – CvRChameleon Feb 25 '21 at 08:34
  • I faced the same InvalidOperationException while running a standard C# application in VS Code. As per my observation, the launch.json file containing the "console" property is created only for .Net 5+ and .Net Core applications from debug settings. The launch.json file that's created for standard C# applications neither contains nor supports the "console" property. Does it affect a standard C# application in any way? If yes, then how to get around it? Please explain. – priyamtheone Jul 11 '23 at 18:30
3

I deployed my webjob to Azure, and received similar error. I fixed the issue by following check;

if (!Console.IsInputRedirected && Console.KeyAvailable)
                {
                    string read = await Console.In.ReadLineAsync();

                    if (read == "f")
                    { // run your logic ...
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
2

Something that I have used in my own code (which as far as I can see is not mentioned in the linked question) is Console.In which interacts directly with the standard input.
I have tested it in my own project with both Windows CMD and openSUSE KTerm and it works as expected.

My code is specifically:
Console.In.ReadLineAsync().GetAwaiter().GetResult();
because it's in the IO handling BackgroundWorker which is running Async.
Yours may be able to get away with Console.In.ReadKey();, though YMMV as I've not tested anything except ReadLine.

Robere Starkk
  • 91
  • 1
  • 6
  • so essentially what is the solution? – Spartaok Nov 14 '18 at 11:49
  • 2
    Essentially the solution would be ```Console.In.Readkey();``` instead of ```System.Console.ReadKey()``` because in the off chance that console input was redirected, `Console.In` would access it where `Console` by itself may not. – Robere Starkk Apr 05 '19 at 00:58