简体   繁体   English

如何在控制台应用程序中隐藏输出(而不是窗口)?

[英]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; 如果直接传递nullConsole.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. 这允许您在一个地方定义功能,每个“子”类将继承此功能。

You could use log4net to define the items you want to log. 您可以使用log4net来定义要记录的项目。 It allows you to log regarding log level, regarding which class you want to log and so on. 它允许您记录有关日志级别,关于要记录的类等的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从AC#WPF应用程序动态隐藏控制台窗口? - How to hide console window dynamically from a c# wpf application? 如何使用Windows Task Scheduler隐藏控制台应用程序窗口 - How to hide console application window with windows task scheduler 如何在 Gtk# Windows 应用程序中隐藏控制台窗口 - How to hide console window in Gtk# Windows Application 如何在 forms 应用程序中显示控制台输出/窗口? - How do I show a console output/window in a forms application? 是否可以静默运行 .NET Core 控制台应用程序(隐藏控制台窗口)? - Is it possible to run a .NET Core console application silently (hide console window)? 显示/隐藏 C# 控制台应用程序的控制台窗口 - Show/Hide the console window of a C# console application 如何在控制台应用程序中打开一个窗口? - How to open a window in console application? 将控制台应用程序的输出同时记录到文本文件和控制台窗口中 - Logging Output of Console Application to a text file and Console Window at the same time 动态编译Windows应用程序时如何隐藏控制台窗口? - How do I hide the console window when dynamically compiling a windows application? 如何隐藏另一个可执行文件的控制台窗口 - how to hide console window of another executable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM