Search code examples
c#console.readkey

Can I bypass Console.ReadKey() issue when input is redirected?


I am a C# teacher and I wrote some automated HW checker for my students. The students write C# Console Applications. My HW checker is based on input redirection so I can test their code on my own generated input.

The problem is that students sometimes end their program with a Console.ReadKey() instruction (They do so just to make the execution window not close when they ran the program under F5 - Debug). The Console.ReadKey() crashes when ran under input redirection with the following exception:

System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file.

Do I have any way to "bypass" this problem (without altering the students code)? Maybe tell Console to ignore ReadKey instructions?


Solution

  • If the executables are in IL, you can create an easy application that uses ILDASM.

    The key point is: disassemble the executable with ILDASM into a text file/stream, look for any call to Console.Read and remove it, than recompile it and run.