6

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.

Community
  • 1
  • 1
Peter
  • 47,963
  • 46
  • 132
  • 181
  • So are you using the console for debug issues or logging? – Joe Phillips Apr 19 '09 at 22:33
  • Just a kind of playgarden, it's my program to do tests. I only use the wpfstuff when I'm testing out wpf, otherwise I just print to the console, by coincidence, i wanted to clear the console, never thought i wouldn't work. Of course I can start a console app without that prob, but I want to be able to do that in wpf in the future, or, at least, grasp the problem. – Peter Apr 19 '09 at 22:37

7 Answers7

6

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.

Zignd
  • 6,896
  • 12
  • 40
  • 62
M4N
  • 94,805
  • 45
  • 217
  • 260
  • It's a wpf test app. Indeed, when trying helloWorld kinda stuff in consoleApp, I don't have a problem. – Peter Apr 19 '09 at 22:29
  • 2
    Dead link. If that was the direction to a solution, it should definitely be updated. – OhBeWise Aug 19 '15 at 15:52
3

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
  • 733,204
  • 149
  • 1,241
  • 1,454
2

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

Andy White
  • 86,444
  • 48
  • 176
  • 211
  • + since pointing out the commands are equivalent, but please, read updates , any help is welcome – Peter Apr 19 '09 at 22:33
1

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
0

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
  • 28,471
  • 61
  • 196
  • 289
0

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 Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • 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 Apr 19 '09 at 22:32
0

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)

Community
  • 1
  • 1
MichaC
  • 2,834
  • 2
  • 20
  • 20