Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if output is redirected

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.

like image 699
Nippysaurus Avatar asked Jul 23 '09 04:07

Nippysaurus


People also ask

What is output redirection?

Output redirection is used to put output of one command into a file or into another command.

Can input be redirected?

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.

What is meant by output redirection and input redirection?

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.


2 Answers

From .NET Framework 4.5 onwards you can use the Console.IsOutputRedirected property to detect this.

There's also Console.IsErrorRedirected.

like image 122
Matt Brooks Avatar answered Sep 24 '22 23:09

Matt Brooks


As it turns out, it soon will be available in the elegant and cross-platform way: link.

like image 33
konrad.kruczynski Avatar answered Sep 22 '22 23:09

konrad.kruczynski