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

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

问题描述

我用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.

推荐答案

您不能。重定向输出是完全和完全执行程序的范围之内。您可以但是添加一个命令行选项来禁用pretty的输出,以便它可以用来重定向了。

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.

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

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