C# ReadKey crashing console app when called using <nul

社会主义新天地 提交于 2020-01-13 09:22:09

问题


So I decided to start programming in C#, and one of the things I did was to create a "pausec.exe" (a pause.exe clone). It works, but when calling it like:

< nul pausec  

...it crashes. The error I get - translated from Spanish to the best of my knowledge - goes like this:

Unhandled exception: System.InvalidOperationException: Can't read keys when any of the applications doesn't have a console or when the console input has been redirected from a file. Try with Console.Read.

And the stacktrace telling me where the error is:

 in System.Console.ReadKey(Boolean intercept)  
 in System.Console.ReadKey()  
 in pausec.Program.Main(String[] args)

This is the code I'm running:

using System;

namespace pausec
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
            Console.Write("\n");
        }
    }
}

I'm wondering if there's a workaround for this, maybe even a way to ignore the ReadKey when using < nul?

Any help is appreciated.
Thanks in advance

UPDATE: Found a way, by removing the intercept (as Alberto Solano suggested) and then, adding Console.Write("\b \b"); after the ReadKey method, it works. Weird thing is, when I copy the application to my desktop, it doesn't wait for user input and closes automatically.

UPDATE 2: It works perfectly now. Thank you all for answering!


回答1:


The console has a method that you can check to see if stdin has been redirected.

public static bool IsInputRedirected { get; }



回答2:


Your program throws that exception because, with Console.ReadKey(true);, as stated in the MSDN documentation:

If the intercept parameter is true, the pressed key is intercepted and not displayed in the console window; otherwise, the pressed key is displayed.

You're not reading or "listening" to any key pressed in the keyboard, and then there's no key to intercept and to not display in the console window.

If you want just to press any key to close the program, use:

Console.ReadKey(); //this won't intercept any key pressed

or

Console.ReadLine();

UPDATE: You asked in the comment how to hide the key pressed by the user. This code should do the trick:

ConsoleKeyInfo cki;
Console.Write("Press any key to continue . . . ");
cki = Console.ReadKey(true);



回答3:


I have faced the same error.

So i just checked my project Output Type.

ProjectName->RightClick->Properties

there you can see your output type.

So i change that into Console application because in my case it was seems to be windows application before. Then its works fine.




回答4:


/// <summary>
/// Written by fredrik92 (http://social.msdn.microsoft.com/Forums/vstudio/en-US/08163199-0a5d-4057-8aa9-3a0a013800c7/how-to-write-a-command-like-pause-to-console?forum=csharpgeneral)
/// Writes a message to the console prompting the user to press a certain key in order to exit the current program.
/// This procedure intercepts all keys that are pressed, so that nothing is displayed in the Console. By default the
/// Enter key is used as the key that has to be pressed.
/// </summary>
/// <param name="key">The key that the Console will wait for. If this parameter is omitted the Enter key is used.</param>    
public static void WriteKeyPressForExit(ConsoleKey key = ConsoleKey.Enter)
{
    Console.WriteLine();
    Console.WriteLine("Press the {0} key on your keyboard to exit . . .", key);
    while (Console.ReadKey(intercept: true).Key != key) { }
}


来源:https://stackoverflow.com/questions/19939666/c-sharp-readkey-crashing-console-app-when-called-using-nul

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!