I have a console application written in C# which processes some data then prints the results. Until the results are available there is a little animation ( / - \ | ) and progress percentage ( xx% ) which is constantly updating.
Obviously if the user redirects the output of my application this will be printed to the text file where output is redirected to. If possible I would like to avoid this.
So is there a way to detect if the output of my application is redirected? I am considering only showing the progress indicators when run with a specified parameter, but I'm hoping someone will have a good answer for me.
Output redirection is used to put output of one command into a file or into another command.
Input Redirection Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file.
Input/Output (I/O) redirection in Linux refers to the ability of the Linux operating system that allows us to change the standard input ( stdin ) and standard output ( stdout ) when executing a command on the terminal. By default, the standard input device is your keyboard and the standard output device is your screen.
From .NET Framework 4.5 onwards you can use the Console.IsOutputRedirected
property to detect this.
There's also Console.IsErrorRedirected
.
As it turns out, it soon will be available in the elegant and cross-platform way: link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With