7

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!

Alberto Solano
  • 7,454
  • 3
  • 33
  • 57
  • Tribal knowledge unrecognized. Can you explain what `pause.exe` is? – P.Brian.Mackey Nov 12 '13 at 21:00
  • @P.Brian.Mackey pause.exe is a cmd command that displays the "Press any key to continue . . ." message, waits for you to do so, and then continues with the code. –  Nov 12 '13 at 21:08

4 Answers4

7

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

public static bool IsInputRedirected { get; }
Gary Walker
  • 8,097
  • 2
  • 14
  • 39
  • Ahhh...but only in v4.5 of the CLR. That property does not exist in earlier versions of the CLR: http://msdn.microsoft.com/en-us/library/system.console.isinputredirected(v=vs.110).aspx – Nicholas Carey Nov 12 '13 at 21:48
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.

User6667769
  • 645
  • 1
  • 5
  • 22
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);
Alberto Solano
  • 7,454
  • 3
  • 33
  • 57
  • This worked, but the only issue is that I was looking for a way to hide the key pressed. If there's no way, though, this'll have to do. –  Nov 12 '13 at 21:17
  • I updated my question too. I'll try you method too, but I found a way using a Console.Write("\b \b") command. EDIT: Your method seems to solve the weird 'press-enter-and-the-first-letter-gets-deleted' issue. –  Nov 12 '13 at 21:48
  • (Edit timed out) I tried using –  Nov 12 '13 at 21:53
  • @ALTOhDude I tested the code with a console application of mine. What is ' – Alberto Solano Nov 12 '13 at 21:59
  • @ALTOhDude Are you trying to do this [How to redirect stderr to null in cmd.exe](http://stackoverflow.com/questions/4507312/how-to-redirect-stderr-to-null-in-cmd-exe) ? – Alberto Solano Nov 12 '13 at 22:01
0
/// <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) { }
}
Recursor
  • 522
  • 4
  • 18
  • It allows you to choose the key you want them to press rather than being any key, but you can easily modify it to be anykey. – Recursor Nov 12 '13 at 20:59