tags:

views:

989

answers:

7

Just that. I found a similar question here : http://stackoverflow.com/questions/377927/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.

+2  A: 

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.

JaredPar
update : that threw exception
Peter
what exception did it throw?
JaredPar
IOException (The handle is invalid), I made an update in the question
Peter
+2  A: 

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

Andy White
+ since pointing out the commands are equivalent, but please, read updates , any help is welcome
Peter
+3  A: 

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

Andrew Hare
update : that threw exception
Peter
A: 

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.

Judah Himango
Martin suggested the error appears in winforms or Asp net. This is wpf and could be a problem too I guess, but even then, the question stands (I can use the console to write to and read from after all)
Peter
+2  A: 

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.

M4N
It's a wpf test app. Indeed, when trying helloWorld kinda stuff in consoleApp, I don't have a problem.
Peter
+1 for most probably showing the direction to the solution.
Peter
A: 

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)

MichaC
A: 

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.

djangofan