如何清除 c#.net 中的控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/766173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:18:23  来源:igfitidea点击:

How to clear the Console in c#.net?

c#wpfconsole

提问by Peter

Just that. I found a similar question here : c# console, Console.Clear problem

只是。我在这里发现了一个类似的问题:c# 控制台,Console.Clear 问题

but that didn't answer the question.

但这并没有回答问题。

UPDATES :

更新 :

Console.Clear() throws : IOException (The handle is invalid)

Console.Clear() 抛出:IOException(句柄无效)

The app is a WPF app. Writing to the console however is no problem at all, nor is reading from.

该应用程序是一个 WPF 应用程序。然而,写入控制台完全没有问题,读取也没有问题。

采纳答案by M4N

Console.Clear()works in a console application.

Console.Clear()在控制台应用程序中工作。

When Calling Console.Clear()in a ASP.NET web site project or in a windows forms application, then you'll get the IOException.

Console.Clear()在 ASP.NET 网站项目或 Windows 窗体应用程序中调用时,您将获得 IOException。

What kind of application do you have?

你有什么样的应用程序?

Update:

更新:

I'm not sure if this will help, but as you can read in this forum thread, Console.Clear()throws an IOException if the console output is being redirected. Maybe this is the case for WPF applications? The article describes how to check whether the console is being redirected.

我不确定这是否会有所帮助,但正如您在此论坛帖子中所读到的那样,Console.Clear()如果控制台输出被重定向,则会引发 IOException。也许这就是 WPF 应用程序的情况?本文介绍了如何检查控制台是否正在被重定向。

回答by JaredPar

Try

尝试

Console.Clear();

EDIT

编辑

Are you trying this method on a non-Console application? If so that would explain the error. Other types of applications, ASP.Net projects, WinForms, etc ... don't actually create a console for writing. So the Clear has nothing to operate on and throws an exception.

您是否在非控制台应用程序上尝试此方法?如果是这样,那将解释错误。其他类型的应用程序、ASP.Net 项目、WinForms 等……实际上并不创建用于编写的控制台。所以 Clear 没有任何操作并抛出异常。

回答by Andy White

Console.Clear() - this is the equivalent of the "cls" command.

Console.Clear() - 这相当于“cls”命令。

回答by Andrew Hare

Try Console.Clear()- it has been available since .NET 2.0.

尝试Console.Clear()- 它从 .NET 2.0 开始可用。

回答by Judah Gabriel Himango

Console.Clear() will do the trick. It should not throw an IOException. If it is, there's something else going on that you're not telling us, in which case you should show us some code.

Console.Clear() 可以解决问题。它不应该抛出 IOException。如果是,那么您没有告诉我们其他事情,在这种情况下,您应该向我们展示一些代码。

回答by MichaC

Are you using some artificial means, like what is described hereto display a console window? I tried creating a WPF application then changing the application output type in its properties Project->Properties... to Console Application. Once I did that a console window popped up when I started my application and I could write to it and call Console.Clear() without any exceptions. (the basic idea is explained here, although my properties UI was slightly different than what is described)

您是否使用了一些人为的方法,例如此处描述的显示控制台窗口的方法?我尝试创建一个 WPF 应用程序,然后在其属性 Project-> 中更改应用程序输出类型属性...到控制台应用程序。一旦我这样做了,当我启动我的应用程序时会弹出一个控制台窗口,我可以写入它并毫无例外地调用 Console.Clear() 。(这里解释了基本思想,尽管我的属性 UI 与所描述的略有不同)

回答by djangofan

Do you really need to clear it? You could also create your own "cls" command that prints out a 100 blank lines to the console and makes it "appear" to have cleared. just an idea.

你真的需要清除它吗?您还可以创建自己的“cls”命令,将 100 行空白行打印到控制台并使其“看起来”已清除。只是一个想法。