I want to write a console application that have a different behavior depending if the input is coming from keyboard or from, say, a file.
Is it possible? What's the most elegant way to do it in C#?
I want to write a console application that have a different behavior depending if the input is coming from keyboard or from, say, a file.
Is it possible? What's the most elegant way to do it in C#?
Lorenz answer is a good beginning, but unfortunately can be used only a inspiration. There are more modes for running a console application.
Standard run (in console, without any redirection)
Everything work as expected in console.
Executing with redirection from console with standard input and/or standard output redirection
e.g.
type input_file.txt | application.exe
(in Windows), orapplication.exe <input_file.txt
for input redirection(replace
type
withcat
in Linux)or
application.exe | grep pattern
orapplication.exe >output_file.txt
for output redirectionor
type input_file.txt | application.exe | grep pattern
orapplication.exe <input_file.txt >output_file.txt
for input and output redirectionExecuting with redirection from console with standard output and error output redirection
e.g.
application.exe >output_file.txt 2>error_file.txt
Executing with hidden console and redirected input/output/error
e.g. from a GUI application (console is not Visible at all)
Executing with hidden console without redirection of input/output/error
Each of these mode has it's own 'features'. The
Console.WindowHeight
andConsole.WindowWidth
work in Windows for the 1st and 2nd mode in the standard way. In Linux the return value in 2nd and 3rd mode is zero. Therefore in Linux you can not detect input only redirecting.Therefore the code from Lorenz answer can not be used for detection of redirection in all cases. The
IOException
when readingConsole.WindowHeight
orConsole.WindowWidth
is thrown only when there is no output to console (e.g. 3rd mode) and only for Windows.To detect input redirection (in Windows only) use this function:
For all other redirection and operating systems... try to experiment how to detect them. Different console properties and functions 'work' (throw exception, or zero return values) for different modes.
Tested on
Windows 7 .NET Framework 4 Client Profile
andMono JIT compiler version 4.2.1 (Debian 4.2.1.102+dfsg2-7ubuntu4)
.Interestingly, when a pipe is open, the
System.Console.WindowHeight
andSystem.Console.WindowWidth
Parameters are zero, which I found out due to severalArgumentOutOfRangeException
's in code paths that did not care for the console size being zero.Crossplatform: The behavior is the same under MS dotNET and Mono on Linux and Windows (I haven't tried it on a Mac).
When either STDIN or STDOUT are piped, the console size is set to 0. Thus building on Hans's implementation, my code is as follows:
Update 2016: Added exception handling to the
IsConsoleSizeZero
Code to improve the usability of the code in a wider context.The code still seems to work well, at least speaking from experience whilst using MonoDevelop / Xamarin Studio.
Related:
MSDN: KeyAvailable
Setting System.Console.WindowHeight throws an System.NotSupportedException under Mono.
You can find out by p/invoking the Windows FileType() API function. Here's a helper class:
Usage:
UPDATE: these methods were added to the Console class in .NET 4.5. Without attribution I might add :( Simply use the corresponding method instead of this helper class.
https://msdn.microsoft.com/en-us/library/system.console.isoutputredirected.aspx https://msdn.microsoft.com/en-us/library/system.console.isinputredirected.aspx https://msdn.microsoft.com/en-us/library/system.console.iserrorredirected.aspx
Since framework 4.5 exists the property Console.IsInputRedirected. 8-)
See MSDN
https://msdn.microsoft.com/de-de/library/system.console.isinputredirected(v=vs.110).aspx