[英]How to hide output (not the window) in a console application?
Well, let me make it clear again. 好吧,让我再说清楚。 I have a console application with many classes, in each class there is at least one Console.WriteLine("text") line. 我有一个包含许多类的控制台应用程序,在每个类中至少有一个Console.WriteLine(“text”)行。 I defined some arguments that when running the application, it output exactly what I wrote in classes. 我定义了一些参数,在运行应用程序时,它输出的正是我在类中编写的内容。 There, I want to define an argument that when I run the app with this argument, there will be no output on the console screen but the console window still appears. 在那里,我想定义一个参数,当我使用此参数运行应用程序时,控制台屏幕上将没有输出但控制台窗口仍然出现。 The argument maybe something like "-s". 这个论点可能就像“-s”。 So is there any way I can do this without using the "if" condition in each class and passing an parameter through classes? 那么,如果不在每个类中使用“if”条件并通过类传递参数,我能做到这一点吗? Many thanks. 非常感谢。
P/S: I've searched thru the internet and found out some articles related to this, but most of the answers were to hide the console window. P / S:我通过互联网搜索并发现了一些与此相关的文章,但大多数答案都是隐藏控制台窗口。 I think this question should be what the others meant. 我认为这个问题应该是其他人的意思。
Use Console.SetOut ( http://msdn.microsoft.com/en-us/library/system.console.setout.aspx ) and pass in a "null" text writer. 使用Console.SetOut( http://msdn.microsoft.com/en-us/library/system.console.setout.aspx )并传入“null”文本编写器。 The idea on -s
perform a Console.SetOut(new StreamWriter())
. -s
上的想法执行Console.SetOut(new StreamWriter())
。
The example for the Console.SetOut
method will help you get started. Console.SetOut
方法的示例将帮助您入门。
In addition, you can easily add logging using this method. 此外,您可以使用此方法轻松添加日志记录。
The Console.SetOut
method throws a ArgumentNullException
if you pass null
directly; 如果直接传递null
则Console.SetOut
方法抛出ArgumentNullException
; same for StreamWriter
(if you pass a stream or if you pass a string . 对于StreamWriter
(如果您传递流或传递字符串 。
What I use is passing the special Stream.Null
field to a StreamWriter
: 我使用的是将特殊的Stream.Null
字段传递给StreamWriter
:
System.Console.SetOut(new System.IO.StreamWriter(System.IO.Stream.Null);
This redirect the stream to what is essentially the null device ( /dev/null
on *NIX machines). 这将流重定向到本质上为空设备(* NIX机器上的/dev/null
)。
Use these methods to redirect output (programmatically): 使用这些方法重定向输出(以编程方式):
Console.SetIn(...);
Console.SetOut(...);
Console.SetError(...);
app.exe > nul
redirects output to nowhere. 将输出重定向到无处。 FYI: Using command redirection operators 仅供参考: 使用命令重定向运算符
This worked pretty well for me !: 这对我来说效果很好!:
Console.SetOut(StreamWriter.Null);
Console.SetError(StreamWriter.Null);
This doesn't work: 这不起作用:
Console.SetOut(null);
Console.SetError(null);
It results in an ArgumentNullException
. 它会导致ArgumentNullException
。
This doesn't work: 这不起作用:
Console.SetOut(new StreamWriter(null));
Console.SetError(new StreamWriter(null));
Because it is ambiguous between new StreamWriter(string path)
and new StreamWriter(Stream stream)
. 因为new StreamWriter(string path)
和new StreamWriter(Stream stream)
之间不明确。
And finally , neither of these work: 最后 ,这些都不起作用:
Console.SetOut(new StreamWriter(default(string)));
Console.SetError(new StreamWriter(default(Stream)));
Because they also result in an ArgumentNullException
. 因为它们也会导致ArgumentNullException
。
Have you come across Console.Clear() , you could call this in a way that makes it appear no text at all was output. 您是否遇到过Console.Clear() ,您可以通过一种方式调用它,使其看起来根本没有输出任何文本。
As for avoiding having to separately define this in each class you should look at inheritance. 至于避免在每个类中单独定义它,你应该看看继承。 This allows you to define the functionality in one place and each 'sub' class will inherit this. 这允许您在一个地方定义功能,每个“子”类将继承此功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.