检查输出是否重定向 [英] Check if output is redirected

查看:152
本文介绍了检查输出是否重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的控制台应用程序,它处理一些数据,然后打印结果。在结果可用之前,有一个不断更新的动画(/ - \ |)和进度百分比(xx%)。

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.

推荐答案

你不能。重定向输出完全和完全超出执行程序的范围。

You can't. Redirected output is totally and wholly outside the scope of the executing program. You can however add a commandline option to disable your pretty output so that it can be used redirected too.

可以添加命令行选项以禁用漂亮的输出,以便重定向。

You CAN (now)... with .NET 4.5 onwards (see Matt Brooks' answer).

这篇关于检查输出是否重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆