问题
Just that. I found a similar question here : c# console, Console.Clear problem
but that didn't answer the question.
UPDATES :
Console.Clear() throws : IOException (The handle is invalid)
The app is a WPF app. Writing to the console however is no problem at all, nor is reading from.
回答1:
Console.Clear()
works in a console application.
When Calling Console.Clear()
in a ASP.NET web site project or in a windows forms application, then you'll get the 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.
回答2:
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.
回答3:
Console.Clear() - this is the equivalent of the "cls" command.
回答4:
Try Console.Clear() - it has been available since .NET 2.0.
回答5:
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.
回答6:
Are you using some artificial means, like what is described here to 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)
回答7:
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.
来源:https://stackoverflow.com/questions/766173/how-to-clear-the-console-in-c-net